-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Devrel 1074 import taxonomies #9
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,39 @@ | ||
import { TaxonomyContracts } from "@kontent-ai/management-sdk"; | ||
|
||
import { zip } from "../../../utils/array.js"; | ||
import { serially } from "../../../utils/requests.js"; | ||
import { EntityDefinition } from "../entityDefinition.js"; | ||
|
||
export const taxonomiesExportEntity: EntityDefinition<ReadonlyArray<TaxonomyContracts.ITaxonomyContract>> = { | ||
export const taxonomiesEntity: EntityDefinition<ReadonlyArray<TaxonomyContracts.ITaxonomyContract>> = { | ||
name: "taxonomies", | ||
fetchEntities: client => client.listTaxonomies().toAllPromise().then(res => res.data.items.map(t => t._raw)), | ||
serializeEntities: taxonomies => JSON.stringify(taxonomies), | ||
importEntities: () => { throw new Error("Not supported yet.")}, | ||
deserializeEntities: () => { throw new Error("Not supported yet.")}, | ||
importEntities: async (client, fileTaxonomies, context) => { | ||
const projectTaxonomies = await serially<ReadonlyArray<() => Promise<TaxonomyContracts.ITaxonomyContract>>>( | ||
fileTaxonomies.map(taxonomy => () => | ||
client | ||
.addTaxonomy() | ||
.withData(addExternalIds(taxonomy)) | ||
.toPromise() | ||
.then(res => res.data._raw)) | ||
); | ||
|
||
return { | ||
...context, | ||
taxonomyGroupIdsByOldIds: new Map(zip(fileTaxonomies.map(t => t.id), projectTaxonomies.map(t => t.id))), | ||
taxonomyTermIdsByOldIds: new Map(zip(fileTaxonomies.flatMap(t => t.terms), projectTaxonomies.flatMap(t => t.terms)).flatMap(extractTermIdsEntries)), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we need two Maps for taxonomies? we can have the same internal ID for taxonomy group and term? I think that it is not probable.. if not, what is the advantage having map for groups and terms? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought it would be a better separation. You never have an id that can be a group or a term. In content types you reference groups in variants you reference terms so why not have them in two maps. |
||
}; | ||
}, | ||
deserializeEntities: JSON.parse, | ||
}; | ||
|
||
const addExternalIds = (taxonomy: TaxonomyContracts.ITaxonomyContract): TaxonomyContracts.ITaxonomyContract => ({ | ||
...taxonomy, | ||
external_id: taxonomy.external_id ?? taxonomy.codename, | ||
terms: taxonomy.terms.map(addExternalIds), | ||
}); | ||
|
||
const extractTermIdsEntries = ([fileTaxonomy, projectTaxonomy]: readonly [TaxonomyContracts.ITaxonomyContract, TaxonomyContracts.ITaxonomyContract]): ReadonlyArray<readonly [string, string]> => [ | ||
[fileTaxonomy.id, projectTaxonomy.id] as const, | ||
...zip(fileTaxonomy.terms, projectTaxonomy.terms).flatMap(extractTermIdsEntries), | ||
]; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
export const zip = <T1 extends ReadonlyArray<any>, T2 extends ReadonlyArray<any>>(arr1: T1, arr2: T2): Zip<T1, T2> => | ||
arr1 | ||
.slice(0, Math.min(arr1.length, arr2.length)) | ||
.map((el1, i) => [el1, arr2[i]] as const) as unknown as Zip<T1, T2>; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is really hard to follow.. maybe adding some comments, why is the array different from Tuple, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You mean on the type-level or where? |
||
type Zip<T1 extends ReadonlyArray<any>, T2 extends ReadonlyArray<any>> = | ||
true extends IsEmptyTuple<T1> | IsEmptyTuple<T2> | ||
? readonly [] | ||
: [IsNonEmptyTuple<T1>, IsNonEmptyTuple<T2>] extends [true, true] | ||
? ZipTuples<T1, T2> | ||
: ZipArrays<T1, T2>; | ||
|
||
type IsEmptyTuple<T extends ReadonlyArray<any>> = T extends readonly [] ? true : false; | ||
|
||
type IsNonEmptyTuple<T extends ReadonlyArray<any>> = T extends readonly [any, ...ReadonlyArray<any>] ? true : false; | ||
|
||
/** | ||
* Handles zip of two types where at least one is an array of unknown length. | ||
*/ | ||
type ZipArrays<T1 extends ReadonlyArray<any>, T2 extends ReadonlyArray<any>> = ReadonlyArray<readonly [T1[number], T2[number]]>; | ||
|
||
/** | ||
* Handles zip of two tuples (arrays of known length and exact types for different positions). | ||
* This type expects two tuples and doesn't work well with arrays of unknown length. | ||
*/ | ||
type ZipTuples<T1 extends ReadonlyArray<any>, T2 extends ReadonlyArray<any>, Accum extends ReadonlyArray<readonly [any, any]> = readonly []> = | ||
[T1, T2] extends [readonly [infer First1, ...infer Rest1 extends ReadonlyArray<any>], readonly [infer First2, ...infer Rest2 extends ReadonlyArray<any>]] | ||
? ZipTuples<Rest1, Rest2, readonly [...Accum, readonly [First1, First2]]> | ||
: Accum; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so we can assume that the order of taxonomies in the project are the same that are in the file? Even though that's probably true and makes sense, is there no possibility that the MAPI response can't mess this up and change the order?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The order of taxonomies is intentional, it is not a dictionary, but a list. You can even reorder them in the UI (not sure about MAPI). So I would be really surprised if it reordered them from what I sent it.