@prismicio/client 7.9.0 → 7.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/BaseClient.cjs +108 -0
- package/dist/BaseClient.cjs.map +1 -0
- package/dist/BaseClient.d.ts +152 -0
- package/dist/BaseClient.js +108 -0
- package/dist/BaseClient.js.map +1 -0
- package/dist/Client.cjs +1217 -0
- package/dist/Client.cjs.map +1 -0
- package/dist/Client.d.ts +924 -0
- package/dist/Client.js +1217 -0
- package/dist/Client.js.map +1 -0
- package/dist/Migration.cjs +318 -0
- package/dist/Migration.cjs.map +1 -0
- package/dist/Migration.d.ts +213 -0
- package/dist/Migration.js +318 -0
- package/dist/Migration.js.map +1 -0
- package/dist/WriteClient.cjs +512 -0
- package/dist/WriteClient.cjs.map +1 -0
- package/dist/WriteClient.d.ts +314 -0
- package/dist/WriteClient.js +512 -0
- package/dist/WriteClient.js.map +1 -0
- package/dist/createClient.cjs +2 -1215
- package/dist/createClient.cjs.map +1 -1
- package/dist/createClient.d.ts +2 -1003
- package/dist/createClient.js +1 -1214
- package/dist/createClient.js.map +1 -1
- package/dist/createMigration.cjs +6 -0
- package/dist/createMigration.cjs.map +1 -0
- package/dist/createMigration.d.ts +24 -0
- package/dist/createMigration.js +6 -0
- package/dist/createMigration.js.map +1 -0
- package/dist/createWriteClient.cjs +6 -0
- package/dist/createWriteClient.cjs.map +1 -0
- package/dist/createWriteClient.d.ts +32 -0
- package/dist/createWriteClient.js +6 -0
- package/dist/createWriteClient.js.map +1 -0
- package/dist/getRepositoryName.cjs +7 -2
- package/dist/getRepositoryName.cjs.map +1 -1
- package/dist/getRepositoryName.d.ts +3 -6
- package/dist/getRepositoryName.js +7 -2
- package/dist/getRepositoryName.js.map +1 -1
- package/dist/index.cjs +14 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +17 -2
- package/dist/index.js +14 -1
- package/dist/index.js.map +1 -1
- package/dist/lib/isMigrationValue.cjs +24 -0
- package/dist/lib/isMigrationValue.cjs.map +1 -0
- package/dist/lib/isMigrationValue.d.ts +62 -0
- package/dist/lib/isMigrationValue.js +24 -0
- package/dist/lib/isMigrationValue.js.map +1 -0
- package/dist/lib/isValue.cjs +53 -0
- package/dist/lib/isValue.cjs.map +1 -0
- package/dist/lib/isValue.d.ts +72 -0
- package/dist/lib/isValue.js +53 -0
- package/dist/lib/isValue.js.map +1 -0
- package/dist/lib/pLimit.cjs +49 -0
- package/dist/lib/pLimit.cjs.map +1 -0
- package/dist/lib/pLimit.d.ts +22 -0
- package/dist/lib/pLimit.js +49 -0
- package/dist/lib/pLimit.js.map +1 -0
- package/dist/lib/resolveMigrationDocumentData.cjs +123 -0
- package/dist/lib/resolveMigrationDocumentData.cjs.map +1 -0
- package/dist/lib/resolveMigrationDocumentData.d.ts +52 -0
- package/dist/lib/resolveMigrationDocumentData.js +123 -0
- package/dist/lib/resolveMigrationDocumentData.js.map +1 -0
- package/dist/lib/validateAssetMetadata.cjs +28 -0
- package/dist/lib/validateAssetMetadata.cjs.map +1 -0
- package/dist/lib/validateAssetMetadata.d.ts +10 -0
- package/dist/lib/validateAssetMetadata.js +28 -0
- package/dist/lib/validateAssetMetadata.js.map +1 -0
- package/dist/package.json.cjs +1 -1
- package/dist/package.json.js +1 -1
- package/dist/types/api/asset/asset.d.ts +177 -0
- package/dist/types/api/asset/tag.d.ts +53 -0
- package/dist/types/api/migration/document.d.ts +60 -0
- package/dist/types/migration/Asset.cjs +33 -0
- package/dist/types/migration/Asset.cjs.map +1 -0
- package/dist/types/migration/Asset.d.ts +109 -0
- package/dist/types/migration/Asset.js +33 -0
- package/dist/types/migration/Asset.js.map +1 -0
- package/dist/types/migration/ContentRelationship.d.ts +17 -0
- package/dist/types/migration/Document.cjs +47 -0
- package/dist/types/migration/Document.cjs.map +1 -0
- package/dist/types/migration/Document.d.ts +94 -0
- package/dist/types/migration/Document.js +47 -0
- package/dist/types/migration/Document.js.map +1 -0
- package/package.json +2 -2
- package/src/BaseClient.ts +330 -0
- package/src/Client.ts +1787 -0
- package/src/Migration.ts +531 -0
- package/src/WriteClient.ts +911 -0
- package/src/createClient.ts +3 -1931
- package/src/createMigration.ts +31 -0
- package/src/createWriteClient.ts +43 -0
- package/src/getRepositoryName.ts +19 -14
- package/src/index.ts +32 -4
- package/src/lib/isMigrationValue.ts +126 -0
- package/src/lib/isValue.ts +196 -0
- package/src/lib/pLimit.ts +106 -0
- package/src/lib/resolveMigrationDocumentData.ts +233 -0
- package/src/lib/validateAssetMetadata.ts +82 -0
- package/src/types/api/asset/asset.ts +211 -0
- package/src/types/api/asset/tag.ts +59 -0
- package/src/types/api/migration/document.ts +84 -0
- package/src/types/migration/Asset.ts +156 -0
- package/src/types/migration/ContentRelationship.ts +36 -0
- package/src/types/migration/Document.ts +157 -0
|
@@ -0,0 +1,911 @@
|
|
|
1
|
+
import { devMsg } from "./lib/devMsg"
|
|
2
|
+
import { pLimit } from "./lib/pLimit"
|
|
3
|
+
import {
|
|
4
|
+
resolveMigrationContentRelationship,
|
|
5
|
+
resolveMigrationDocumentData,
|
|
6
|
+
} from "./lib/resolveMigrationDocumentData"
|
|
7
|
+
|
|
8
|
+
import type {
|
|
9
|
+
Asset,
|
|
10
|
+
PatchAssetParams,
|
|
11
|
+
PatchAssetResult,
|
|
12
|
+
PostAssetParams,
|
|
13
|
+
PostAssetResult,
|
|
14
|
+
} from "./types/api/asset/asset"
|
|
15
|
+
import type {
|
|
16
|
+
AssetTag,
|
|
17
|
+
GetAssetTagsResult,
|
|
18
|
+
PostAssetTagParams,
|
|
19
|
+
PostAssetTagResult,
|
|
20
|
+
} from "./types/api/asset/tag"
|
|
21
|
+
import type { PutDocumentResult } from "./types/api/migration/document"
|
|
22
|
+
import {
|
|
23
|
+
type PostDocumentParams,
|
|
24
|
+
type PostDocumentResult,
|
|
25
|
+
type PutDocumentParams,
|
|
26
|
+
} from "./types/api/migration/document"
|
|
27
|
+
import type { PrismicMigrationAsset } from "./types/migration/Asset"
|
|
28
|
+
import type {
|
|
29
|
+
MigrationDocument,
|
|
30
|
+
PendingPrismicDocument,
|
|
31
|
+
PrismicMigrationDocument,
|
|
32
|
+
} from "./types/migration/Document"
|
|
33
|
+
import type { PrismicDocument } from "./types/value/document"
|
|
34
|
+
|
|
35
|
+
import { PrismicError } from "./errors/PrismicError"
|
|
36
|
+
|
|
37
|
+
import type { FetchParams, RequestInitLike } from "./BaseClient"
|
|
38
|
+
import { Client } from "./Client"
|
|
39
|
+
import type { ClientConfig } from "./Client"
|
|
40
|
+
import type { Migration } from "./Migration"
|
|
41
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
42
|
+
import type { createMigration } from "./createMigration"
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Extracts one or more Prismic document types that match a given Prismic
|
|
46
|
+
* document type. If no matches are found, no extraction is performed and the
|
|
47
|
+
* union of all provided Prismic document types are returned.
|
|
48
|
+
*
|
|
49
|
+
* @typeParam TDocuments - Prismic document types from which to extract.
|
|
50
|
+
* @typeParam TDocumentType - Type(s) to match `TDocuments` against.
|
|
51
|
+
*/
|
|
52
|
+
type ExtractDocumentType<
|
|
53
|
+
TDocuments extends { type: string },
|
|
54
|
+
TDocumentType extends TDocuments["type"],
|
|
55
|
+
> =
|
|
56
|
+
Extract<TDocuments, { type: TDocumentType }> extends never
|
|
57
|
+
? TDocuments
|
|
58
|
+
: Extract<TDocuments, { type: TDocumentType }>
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Utility type to construct events reported by the migration process.
|
|
62
|
+
*/
|
|
63
|
+
type MigrateReporterEvent<
|
|
64
|
+
TType extends string,
|
|
65
|
+
TData = never,
|
|
66
|
+
> = TData extends never
|
|
67
|
+
? { type: TType }
|
|
68
|
+
: {
|
|
69
|
+
type: TType
|
|
70
|
+
data: TData
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* A map of event types and their data reported by the migration process.
|
|
75
|
+
*/
|
|
76
|
+
type MigrateReporterEventMap = {
|
|
77
|
+
start: {
|
|
78
|
+
pending: {
|
|
79
|
+
documents: number
|
|
80
|
+
assets: number
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
end: {
|
|
84
|
+
migrated: {
|
|
85
|
+
documents: number
|
|
86
|
+
assets: number
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
"assets:creating": {
|
|
90
|
+
current: number
|
|
91
|
+
remaining: number
|
|
92
|
+
total: number
|
|
93
|
+
asset: PrismicMigrationAsset
|
|
94
|
+
}
|
|
95
|
+
"assets:created": {
|
|
96
|
+
created: number
|
|
97
|
+
}
|
|
98
|
+
"documents:masterLocale": {
|
|
99
|
+
masterLocale: string
|
|
100
|
+
}
|
|
101
|
+
"documents:creating": {
|
|
102
|
+
current: number
|
|
103
|
+
remaining: number
|
|
104
|
+
total: number
|
|
105
|
+
document: PrismicMigrationDocument
|
|
106
|
+
}
|
|
107
|
+
"documents:created": {
|
|
108
|
+
created: number
|
|
109
|
+
}
|
|
110
|
+
"documents:updating": {
|
|
111
|
+
current: number
|
|
112
|
+
remaining: number
|
|
113
|
+
total: number
|
|
114
|
+
document: PrismicMigrationDocument
|
|
115
|
+
}
|
|
116
|
+
"documents:updated": {
|
|
117
|
+
updated: number
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Available event types reported by the migration process.
|
|
123
|
+
*/
|
|
124
|
+
type MigrateReporterEventTypes = keyof MigrateReporterEventMap
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* All events reported by the migration process. Events can be listened to by
|
|
128
|
+
* providing a `reporter` function to the `migrate` method.
|
|
129
|
+
*/
|
|
130
|
+
export type MigrateReporterEvents = {
|
|
131
|
+
[K in MigrateReporterEventTypes]: MigrateReporterEvent<
|
|
132
|
+
K,
|
|
133
|
+
MigrateReporterEventMap[K]
|
|
134
|
+
>
|
|
135
|
+
}[MigrateReporterEventTypes]
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Additional parameters for creating an asset in the Prismic media library.
|
|
139
|
+
*/
|
|
140
|
+
export type CreateAssetParams = {
|
|
141
|
+
/**
|
|
142
|
+
* Asset notes.
|
|
143
|
+
*/
|
|
144
|
+
notes?: string
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Asset credits.
|
|
148
|
+
*/
|
|
149
|
+
credits?: string
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Asset alt text.
|
|
153
|
+
*/
|
|
154
|
+
alt?: string
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Asset tags.
|
|
158
|
+
*/
|
|
159
|
+
tags?: string[]
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Prismic Migration API demo keys.
|
|
164
|
+
*/
|
|
165
|
+
const MIGRATION_API_DEMO_KEYS = [
|
|
166
|
+
"cSaZlfkQlF9C6CEAM2Del6MNX9WonlV86HPbeEJL",
|
|
167
|
+
"pZCexCajUQ4jriYwIGSxA1drZrFxDyFf1S0D1K0P",
|
|
168
|
+
"Yc0mfrkGDw8gaaGKTrzwC3QUZDajv6k73DA99vWN",
|
|
169
|
+
"ySzSEbVMAb5S1oSCQfbVG4mbh9Cb8wlF7BCvKI0L",
|
|
170
|
+
"g2DA3EKWvx8uxVYcNFrmT5nJpon1Vi9V4XcOibJD",
|
|
171
|
+
"CCNIlI0Vz41J66oFwsHUXaZa6NYFIY6z7aDF62Bc",
|
|
172
|
+
]
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Configuration for clients that determine how content is queried.
|
|
176
|
+
*/
|
|
177
|
+
export type WriteClientConfig = {
|
|
178
|
+
/**
|
|
179
|
+
* A Prismic write token that allows writing content to the repository.
|
|
180
|
+
*/
|
|
181
|
+
writeToken: string
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* A Prismic Migration API key that allows working with the Migration API.
|
|
185
|
+
*
|
|
186
|
+
* @remarks
|
|
187
|
+
* If no key is provided, the client will use one of the demo key available
|
|
188
|
+
* which has stricter rate limiting rules enforced.
|
|
189
|
+
*/
|
|
190
|
+
migrationAPIKey?: string
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* The Prismic Asset API endpoint.
|
|
194
|
+
*
|
|
195
|
+
* @defaultValue `"https://asset-api.prismic.io/"`
|
|
196
|
+
*
|
|
197
|
+
* @see Prismic Asset API technical reference: {@link https://prismic.io/docs/asset-api-technical-reference}
|
|
198
|
+
*/
|
|
199
|
+
assetAPIEndpoint?: string
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* The Prismic Migration API endpoint.
|
|
203
|
+
*
|
|
204
|
+
* @defaultValue `"https://migration.prismic.io/"`
|
|
205
|
+
*
|
|
206
|
+
* @see Prismic Migration API technical reference: {@link https://prismic.io/docs/migration-api-technical-reference}
|
|
207
|
+
*/
|
|
208
|
+
migrationAPIEndpoint?: string
|
|
209
|
+
} & ClientConfig
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* A client that allows querying and writing content to a Prismic repository.
|
|
213
|
+
*
|
|
214
|
+
* If used in an environment where a global `fetch` function is unavailable,
|
|
215
|
+
* such as Node.js, the `fetch` option must be provided as part of the `options`
|
|
216
|
+
* parameter.
|
|
217
|
+
*
|
|
218
|
+
* @typeParam TDocuments - Document types that are registered for the Prismic
|
|
219
|
+
* repository. Query methods will automatically be typed based on this type.
|
|
220
|
+
*/
|
|
221
|
+
export class WriteClient<
|
|
222
|
+
TDocuments extends PrismicDocument = PrismicDocument,
|
|
223
|
+
> extends Client<TDocuments> {
|
|
224
|
+
writeToken: string
|
|
225
|
+
migrationAPIKey: string
|
|
226
|
+
|
|
227
|
+
assetAPIEndpoint = "https://asset-api.prismic.io/"
|
|
228
|
+
migrationAPIEndpoint = "https://migration.prismic.io/"
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Creates a Prismic client that can be used to query and write content to a
|
|
232
|
+
* repository.
|
|
233
|
+
*
|
|
234
|
+
* If used in an environment where a global `fetch` function is unavailable,
|
|
235
|
+
* such as in some Node.js versions, the `fetch` option must be provided as
|
|
236
|
+
* part of the `options` parameter.
|
|
237
|
+
*
|
|
238
|
+
* @param repositoryName - The Prismic repository name for the repository.
|
|
239
|
+
* @param options - Configuration that determines how content will be queried
|
|
240
|
+
* from and written to the Prismic repository.
|
|
241
|
+
*
|
|
242
|
+
* @returns A client that can query and write content to the repository.
|
|
243
|
+
*/
|
|
244
|
+
constructor(repositoryName: string, options: WriteClientConfig) {
|
|
245
|
+
super(repositoryName, options)
|
|
246
|
+
|
|
247
|
+
if (typeof globalThis.window !== "undefined") {
|
|
248
|
+
console.warn(
|
|
249
|
+
`[@prismicio/client] Prismic write client appears to be running in a browser environment. This is not recommended as it exposes your write token and Migration API key. Consider using Prismic write client in a server environment only, preferring the regular client for browser environement. For more details, see ${devMsg("avoid-write-client-in-browser")}`,
|
|
250
|
+
)
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
this.writeToken = options.writeToken
|
|
254
|
+
this.migrationAPIKey =
|
|
255
|
+
options.migrationAPIKey ||
|
|
256
|
+
MIGRATION_API_DEMO_KEYS[
|
|
257
|
+
Math.floor(Math.random() * MIGRATION_API_DEMO_KEYS.length)
|
|
258
|
+
]
|
|
259
|
+
|
|
260
|
+
if (options.assetAPIEndpoint) {
|
|
261
|
+
this.assetAPIEndpoint = `${options.assetAPIEndpoint}/`
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
if (options.migrationAPIEndpoint) {
|
|
265
|
+
this.migrationAPIEndpoint = `${options.migrationAPIEndpoint}/`
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Creates a migration release on the Prismic repository based on the provided
|
|
271
|
+
* prepared migration.
|
|
272
|
+
*
|
|
273
|
+
* @param migration - A migration prepared with {@link createMigration}.
|
|
274
|
+
* @param params - An event listener and additional fetch parameters.
|
|
275
|
+
*
|
|
276
|
+
* @see Prismic Migration API technical reference: {@link https://prismic.io/docs/migration-api-technical-reference}
|
|
277
|
+
*/
|
|
278
|
+
async migrate(
|
|
279
|
+
migration: Migration<TDocuments>,
|
|
280
|
+
params: {
|
|
281
|
+
reporter?: (event: MigrateReporterEvents) => void
|
|
282
|
+
} & FetchParams = {},
|
|
283
|
+
): Promise<void> {
|
|
284
|
+
params.reporter?.({
|
|
285
|
+
type: "start",
|
|
286
|
+
data: {
|
|
287
|
+
pending: {
|
|
288
|
+
documents: migration._documents.length,
|
|
289
|
+
assets: migration._assets.size,
|
|
290
|
+
},
|
|
291
|
+
},
|
|
292
|
+
})
|
|
293
|
+
|
|
294
|
+
await this.migrateCreateAssets(migration, params)
|
|
295
|
+
await this.migrateCreateDocuments(migration, params)
|
|
296
|
+
await this.migrateUpdateDocuments(migration, params)
|
|
297
|
+
|
|
298
|
+
params.reporter?.({
|
|
299
|
+
type: "end",
|
|
300
|
+
data: {
|
|
301
|
+
migrated: {
|
|
302
|
+
documents: migration._documents.length,
|
|
303
|
+
assets: migration._assets.size,
|
|
304
|
+
},
|
|
305
|
+
},
|
|
306
|
+
})
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Creates assets in the Prismic repository's media library.
|
|
311
|
+
*
|
|
312
|
+
* @param migration - A migration prepared with {@link createMigration}.
|
|
313
|
+
* @param params - An event listener and additional fetch parameters.
|
|
314
|
+
*
|
|
315
|
+
* @internal This method is one of the step performed by the {@link migrate} method.
|
|
316
|
+
*/
|
|
317
|
+
private async migrateCreateAssets(
|
|
318
|
+
migration: Migration<TDocuments>,
|
|
319
|
+
{
|
|
320
|
+
reporter,
|
|
321
|
+
...fetchParams
|
|
322
|
+
}: { reporter?: (event: MigrateReporterEvents) => void } & FetchParams = {},
|
|
323
|
+
): Promise<void> {
|
|
324
|
+
let created = 0
|
|
325
|
+
for (const [_, migrationAsset] of migration._assets) {
|
|
326
|
+
reporter?.({
|
|
327
|
+
type: "assets:creating",
|
|
328
|
+
data: {
|
|
329
|
+
current: ++created,
|
|
330
|
+
remaining: migration._assets.size - created,
|
|
331
|
+
total: migration._assets.size,
|
|
332
|
+
asset: migrationAsset,
|
|
333
|
+
},
|
|
334
|
+
})
|
|
335
|
+
|
|
336
|
+
const { file, filename, notes, credits, alt, tags } =
|
|
337
|
+
migrationAsset.config
|
|
338
|
+
|
|
339
|
+
let resolvedFile: PostAssetParams["file"] | File
|
|
340
|
+
if (typeof file === "string") {
|
|
341
|
+
let url: URL | undefined
|
|
342
|
+
try {
|
|
343
|
+
url = new URL(file)
|
|
344
|
+
} catch (error) {
|
|
345
|
+
// noop only on invalid URL, fetch errors will throw in the next if statement
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
if (url) {
|
|
349
|
+
// File is a URL, fetch it
|
|
350
|
+
resolvedFile = await this.fetchForeignAsset(
|
|
351
|
+
url.toString(),
|
|
352
|
+
fetchParams,
|
|
353
|
+
)
|
|
354
|
+
} else {
|
|
355
|
+
// File is actual file content, use it as-is
|
|
356
|
+
resolvedFile = file
|
|
357
|
+
}
|
|
358
|
+
} else if (file instanceof URL) {
|
|
359
|
+
// File is a URL instance, fetch it
|
|
360
|
+
resolvedFile = await this.fetchForeignAsset(
|
|
361
|
+
file.toString(),
|
|
362
|
+
fetchParams,
|
|
363
|
+
)
|
|
364
|
+
} else {
|
|
365
|
+
resolvedFile = file
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
const asset = await this.createAsset(resolvedFile, filename, {
|
|
369
|
+
...{ notes, credits, alt, tags },
|
|
370
|
+
...fetchParams,
|
|
371
|
+
})
|
|
372
|
+
|
|
373
|
+
migrationAsset.asset = asset
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
reporter?.({
|
|
377
|
+
type: "assets:created",
|
|
378
|
+
data: {
|
|
379
|
+
created,
|
|
380
|
+
},
|
|
381
|
+
})
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* Creates documents in the Prismic repository's migration release.
|
|
386
|
+
*
|
|
387
|
+
* @param migration - A migration prepared with {@link createMigration}.
|
|
388
|
+
* @param params - An event listener and additional fetch parameters.
|
|
389
|
+
*
|
|
390
|
+
* @internal This method is one of the step performed by the {@link migrate} method.
|
|
391
|
+
*/
|
|
392
|
+
private async migrateCreateDocuments(
|
|
393
|
+
migration: Migration<TDocuments>,
|
|
394
|
+
{
|
|
395
|
+
reporter,
|
|
396
|
+
...fetchParams
|
|
397
|
+
}: { reporter?: (event: MigrateReporterEvents) => void } & FetchParams = {},
|
|
398
|
+
): Promise<void> {
|
|
399
|
+
// Resolve master locale
|
|
400
|
+
const repository = await this.getRepository(fetchParams)
|
|
401
|
+
const masterLocale = repository.languages.find((lang) => lang.is_master)!.id
|
|
402
|
+
reporter?.({
|
|
403
|
+
type: "documents:masterLocale",
|
|
404
|
+
data: {
|
|
405
|
+
masterLocale,
|
|
406
|
+
},
|
|
407
|
+
})
|
|
408
|
+
|
|
409
|
+
const documentsToCreate: PrismicMigrationDocument<TDocuments>[] = []
|
|
410
|
+
// We create an array with non-master locale documents last because
|
|
411
|
+
// we need their master locale document to be created first.
|
|
412
|
+
for (const doc of migration._documents) {
|
|
413
|
+
if (!doc.document.id) {
|
|
414
|
+
if (doc.document.lang === masterLocale) {
|
|
415
|
+
documentsToCreate.unshift(doc)
|
|
416
|
+
} else {
|
|
417
|
+
documentsToCreate.push(doc)
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
let created = 0
|
|
423
|
+
for (const doc of documentsToCreate) {
|
|
424
|
+
reporter?.({
|
|
425
|
+
type: "documents:creating",
|
|
426
|
+
data: {
|
|
427
|
+
current: ++created,
|
|
428
|
+
remaining: documentsToCreate.length - created,
|
|
429
|
+
total: documentsToCreate.length,
|
|
430
|
+
document: doc,
|
|
431
|
+
},
|
|
432
|
+
})
|
|
433
|
+
|
|
434
|
+
// Resolve master language document ID for non-master locale documents
|
|
435
|
+
let masterLanguageDocumentID: string | undefined
|
|
436
|
+
if (doc.masterLanguageDocument) {
|
|
437
|
+
const masterLanguageDocument =
|
|
438
|
+
await resolveMigrationContentRelationship(doc.masterLanguageDocument)
|
|
439
|
+
|
|
440
|
+
masterLanguageDocumentID =
|
|
441
|
+
"id" in masterLanguageDocument ? masterLanguageDocument.id : undefined
|
|
442
|
+
} else if (doc.originalPrismicDocument) {
|
|
443
|
+
const maybeOriginalID =
|
|
444
|
+
doc.originalPrismicDocument.alternate_languages.find(
|
|
445
|
+
({ lang }) => lang === masterLocale,
|
|
446
|
+
)?.id
|
|
447
|
+
|
|
448
|
+
if (maybeOriginalID) {
|
|
449
|
+
masterLanguageDocumentID =
|
|
450
|
+
migration._getByOriginalID(maybeOriginalID)?.document.id
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
const { id } = await this.createDocument(
|
|
455
|
+
// We'll upload documents data later on.
|
|
456
|
+
{ ...doc.document, data: {} },
|
|
457
|
+
doc.title!,
|
|
458
|
+
{
|
|
459
|
+
masterLanguageDocumentID,
|
|
460
|
+
...fetchParams,
|
|
461
|
+
},
|
|
462
|
+
)
|
|
463
|
+
|
|
464
|
+
doc.document.id = id
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
reporter?.({
|
|
468
|
+
type: "documents:created",
|
|
469
|
+
data: { created },
|
|
470
|
+
})
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
/**
|
|
474
|
+
* Updates documents in the Prismic repository's migration release with their
|
|
475
|
+
* patched data.
|
|
476
|
+
*
|
|
477
|
+
* @param migration - A migration prepared with {@link createMigration}.
|
|
478
|
+
* @param params - An event listener and additional fetch parameters.
|
|
479
|
+
*
|
|
480
|
+
* @internal This method is one of the step performed by the {@link migrate} method.
|
|
481
|
+
*/
|
|
482
|
+
private async migrateUpdateDocuments(
|
|
483
|
+
migration: Migration<TDocuments>,
|
|
484
|
+
{
|
|
485
|
+
reporter,
|
|
486
|
+
...fetchParams
|
|
487
|
+
}: { reporter?: (event: MigrateReporterEvents) => void } & FetchParams = {},
|
|
488
|
+
): Promise<void> {
|
|
489
|
+
let i = 0
|
|
490
|
+
for (const doc of migration._documents) {
|
|
491
|
+
reporter?.({
|
|
492
|
+
type: "documents:updating",
|
|
493
|
+
data: {
|
|
494
|
+
current: ++i,
|
|
495
|
+
remaining: migration._documents.length - i,
|
|
496
|
+
total: migration._documents.length,
|
|
497
|
+
document: doc,
|
|
498
|
+
},
|
|
499
|
+
})
|
|
500
|
+
|
|
501
|
+
await this.updateDocument(
|
|
502
|
+
doc.document.id!,
|
|
503
|
+
// We need to forward again document name and tags to update them
|
|
504
|
+
// in case the document already existed during the previous step.
|
|
505
|
+
{
|
|
506
|
+
...doc.document,
|
|
507
|
+
data: await resolveMigrationDocumentData(
|
|
508
|
+
doc.document.data,
|
|
509
|
+
migration,
|
|
510
|
+
),
|
|
511
|
+
},
|
|
512
|
+
fetchParams,
|
|
513
|
+
)
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
reporter?.({
|
|
517
|
+
type: "documents:updated",
|
|
518
|
+
data: {
|
|
519
|
+
updated: migration._documents.length,
|
|
520
|
+
},
|
|
521
|
+
})
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
/**
|
|
525
|
+
* Creates an asset in the Prismic media library.
|
|
526
|
+
*
|
|
527
|
+
* @param file - The file to upload as an asset.
|
|
528
|
+
* @param filename - The filename of the asset.
|
|
529
|
+
* @param params - Additional asset data and fetch parameters.
|
|
530
|
+
*
|
|
531
|
+
* @returns The created asset.
|
|
532
|
+
*/
|
|
533
|
+
private async createAsset(
|
|
534
|
+
file: PostAssetParams["file"] | File,
|
|
535
|
+
filename: string,
|
|
536
|
+
{
|
|
537
|
+
notes,
|
|
538
|
+
credits,
|
|
539
|
+
alt,
|
|
540
|
+
tags,
|
|
541
|
+
...params
|
|
542
|
+
}: CreateAssetParams & FetchParams = {},
|
|
543
|
+
): Promise<Asset> {
|
|
544
|
+
const url = new URL("assets", this.assetAPIEndpoint)
|
|
545
|
+
|
|
546
|
+
const formData = new FormData()
|
|
547
|
+
formData.append(
|
|
548
|
+
"file",
|
|
549
|
+
new File([file], filename, {
|
|
550
|
+
type: file instanceof File ? file.type : undefined,
|
|
551
|
+
}),
|
|
552
|
+
)
|
|
553
|
+
|
|
554
|
+
if (notes) {
|
|
555
|
+
formData.append("notes", notes)
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
if (credits) {
|
|
559
|
+
formData.append("credits", credits)
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
if (alt) {
|
|
563
|
+
formData.append("alt", alt)
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
const asset = await this.fetch<PostAssetResult>(
|
|
567
|
+
url.toString(),
|
|
568
|
+
this.buildAssetAPIQueryParams({
|
|
569
|
+
method: "POST",
|
|
570
|
+
body: formData,
|
|
571
|
+
params,
|
|
572
|
+
}),
|
|
573
|
+
)
|
|
574
|
+
|
|
575
|
+
if (tags && tags.length) {
|
|
576
|
+
return this.updateAsset(asset.id, { tags })
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
return asset
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
/**
|
|
583
|
+
* Updates an asset in the Prismic media library.
|
|
584
|
+
*
|
|
585
|
+
* @param id - The ID of the asset to update.
|
|
586
|
+
* @param params - The asset data to update and additional fetch parameters.
|
|
587
|
+
*
|
|
588
|
+
* @returns The updated asset.
|
|
589
|
+
*/
|
|
590
|
+
private async updateAsset(
|
|
591
|
+
id: string,
|
|
592
|
+
{
|
|
593
|
+
notes,
|
|
594
|
+
credits,
|
|
595
|
+
alt,
|
|
596
|
+
filename,
|
|
597
|
+
tags,
|
|
598
|
+
...params
|
|
599
|
+
}: PatchAssetParams & FetchParams = {},
|
|
600
|
+
): Promise<Asset> {
|
|
601
|
+
const url = new URL(`assets/${id}`, this.assetAPIEndpoint)
|
|
602
|
+
|
|
603
|
+
// Resolve tags if any and create missing ones
|
|
604
|
+
if (tags && tags.length) {
|
|
605
|
+
tags = await this.resolveAssetTagIDs(tags, {
|
|
606
|
+
createTags: true,
|
|
607
|
+
...params,
|
|
608
|
+
})
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
return this.fetch<PatchAssetResult>(
|
|
612
|
+
url.toString(),
|
|
613
|
+
this.buildAssetAPIQueryParams<PatchAssetParams>({
|
|
614
|
+
method: "PATCH",
|
|
615
|
+
body: {
|
|
616
|
+
notes,
|
|
617
|
+
credits,
|
|
618
|
+
alt,
|
|
619
|
+
filename,
|
|
620
|
+
tags,
|
|
621
|
+
},
|
|
622
|
+
params,
|
|
623
|
+
}),
|
|
624
|
+
)
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
/**
|
|
628
|
+
* Fetches a foreign asset from a URL.
|
|
629
|
+
*
|
|
630
|
+
* @param url - The URL of the asset to fetch.
|
|
631
|
+
* @param params - Additional fetch parameters.
|
|
632
|
+
*
|
|
633
|
+
* @returns A file representing the fetched asset.
|
|
634
|
+
*/
|
|
635
|
+
private async fetchForeignAsset(
|
|
636
|
+
url: string,
|
|
637
|
+
params: FetchParams = {},
|
|
638
|
+
): Promise<Blob> {
|
|
639
|
+
const requestInit: RequestInitLike = {
|
|
640
|
+
...this.fetchOptions,
|
|
641
|
+
...params.fetchOptions,
|
|
642
|
+
headers: {
|
|
643
|
+
...this.fetchOptions?.headers,
|
|
644
|
+
...params.fetchOptions?.headers,
|
|
645
|
+
},
|
|
646
|
+
signal:
|
|
647
|
+
params.fetchOptions?.signal ||
|
|
648
|
+
params.signal ||
|
|
649
|
+
this.fetchOptions?.signal,
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
const res = await this.fetchFn(url, requestInit)
|
|
653
|
+
|
|
654
|
+
if (!res.ok) {
|
|
655
|
+
throw new PrismicError("Could not fetch foreign asset", url, undefined)
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
return res.blob()
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
/**
|
|
662
|
+
* {@link resolveAssetTagIDs} rate limiter.
|
|
663
|
+
*/
|
|
664
|
+
private _resolveAssetTagIDsLimit = pLimit()
|
|
665
|
+
|
|
666
|
+
/**
|
|
667
|
+
* Resolves asset tag IDs from tag names.
|
|
668
|
+
*
|
|
669
|
+
* @param tagNames - An array of tag names to resolve.
|
|
670
|
+
* @param params - Whether or not missing tags should be created and
|
|
671
|
+
* additional fetch parameters.
|
|
672
|
+
*
|
|
673
|
+
* @returns An array of resolved tag IDs.
|
|
674
|
+
*/
|
|
675
|
+
private async resolveAssetTagIDs(
|
|
676
|
+
tagNames: string[] = [],
|
|
677
|
+
{ createTags, ...params }: { createTags?: boolean } & FetchParams = {},
|
|
678
|
+
): Promise<string[]> {
|
|
679
|
+
return this._resolveAssetTagIDsLimit(async () => {
|
|
680
|
+
const existingTags = await this.getAssetTags(params)
|
|
681
|
+
const existingTagMap: Record<string, AssetTag> = {}
|
|
682
|
+
for (const tag of existingTags) {
|
|
683
|
+
existingTagMap[tag.name] = tag
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
const resolvedTagIDs = []
|
|
687
|
+
for (const tagName of tagNames) {
|
|
688
|
+
// Tag does not exists yet, we create it if `createTags` is set
|
|
689
|
+
if (!existingTagMap[tagName] && createTags) {
|
|
690
|
+
existingTagMap[tagName] = await this.createAssetTag(tagName, params)
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
// Add tag if found
|
|
694
|
+
if (existingTagMap[tagName]) {
|
|
695
|
+
resolvedTagIDs.push(existingTagMap[tagName].id)
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
return resolvedTagIDs
|
|
700
|
+
})
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
/**
|
|
704
|
+
* Creates a tag in the Asset API.
|
|
705
|
+
*
|
|
706
|
+
* @remarks
|
|
707
|
+
* Tags should be at least 3 characters long and 20 characters at most.
|
|
708
|
+
*
|
|
709
|
+
* @param name - The name of the tag to create.
|
|
710
|
+
* @param params - Additional fetch parameters.
|
|
711
|
+
*
|
|
712
|
+
* @returns The created tag.
|
|
713
|
+
*/
|
|
714
|
+
private async createAssetTag(
|
|
715
|
+
name: string,
|
|
716
|
+
params?: FetchParams,
|
|
717
|
+
): Promise<AssetTag> {
|
|
718
|
+
const url = new URL("tags", this.assetAPIEndpoint)
|
|
719
|
+
|
|
720
|
+
return this.fetch<PostAssetTagResult>(
|
|
721
|
+
url.toString(),
|
|
722
|
+
this.buildAssetAPIQueryParams<PostAssetTagParams>({
|
|
723
|
+
method: "POST",
|
|
724
|
+
body: { name },
|
|
725
|
+
params,
|
|
726
|
+
}),
|
|
727
|
+
)
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
/**
|
|
731
|
+
* Queries existing tags from the Asset API.
|
|
732
|
+
*
|
|
733
|
+
* @param params - Additional fetch parameters.
|
|
734
|
+
*
|
|
735
|
+
* @returns An array of existing tags.
|
|
736
|
+
*/
|
|
737
|
+
private async getAssetTags(params?: FetchParams): Promise<AssetTag[]> {
|
|
738
|
+
const url = new URL("tags", this.assetAPIEndpoint)
|
|
739
|
+
|
|
740
|
+
const { items } = await this.fetch<GetAssetTagsResult>(
|
|
741
|
+
url.toString(),
|
|
742
|
+
this.buildAssetAPIQueryParams({ params }),
|
|
743
|
+
)
|
|
744
|
+
|
|
745
|
+
return items
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
/**
|
|
749
|
+
* Creates a document in the repository's migration release.
|
|
750
|
+
*
|
|
751
|
+
* @typeParam TType - Type of Prismic documents to create.
|
|
752
|
+
*
|
|
753
|
+
* @param document - The document to create.
|
|
754
|
+
* @param documentTitle - The title of the document to create which will be
|
|
755
|
+
* displayed in the editor.
|
|
756
|
+
* @param params - Document master language document ID and additional fetch
|
|
757
|
+
* parameters.
|
|
758
|
+
*
|
|
759
|
+
* @returns The ID of the created document.
|
|
760
|
+
*
|
|
761
|
+
* @see Prismic Migration API technical reference: {@link https://prismic.io/docs/migration-api-technical-reference}
|
|
762
|
+
*/
|
|
763
|
+
private async createDocument<TType extends TDocuments["type"]>(
|
|
764
|
+
document: PendingPrismicDocument<ExtractDocumentType<TDocuments, TType>>,
|
|
765
|
+
documentTitle: string,
|
|
766
|
+
{
|
|
767
|
+
masterLanguageDocumentID,
|
|
768
|
+
...params
|
|
769
|
+
}: { masterLanguageDocumentID?: string } & FetchParams = {},
|
|
770
|
+
): Promise<{ id: string }> {
|
|
771
|
+
const url = new URL("documents", this.migrationAPIEndpoint)
|
|
772
|
+
|
|
773
|
+
const result = await this.fetch<PostDocumentResult>(
|
|
774
|
+
url.toString(),
|
|
775
|
+
this.buildMigrationAPIQueryParams<PostDocumentParams>({
|
|
776
|
+
method: "POST",
|
|
777
|
+
body: {
|
|
778
|
+
title: documentTitle,
|
|
779
|
+
type: document.type,
|
|
780
|
+
uid: document.uid || undefined,
|
|
781
|
+
lang: document.lang,
|
|
782
|
+
alternate_language_id: masterLanguageDocumentID,
|
|
783
|
+
tags: document.tags,
|
|
784
|
+
data: document.data,
|
|
785
|
+
},
|
|
786
|
+
params,
|
|
787
|
+
}),
|
|
788
|
+
)
|
|
789
|
+
|
|
790
|
+
return { id: result.id }
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
/**
|
|
794
|
+
* Updates an existing document in the repository's migration release.
|
|
795
|
+
*
|
|
796
|
+
* @typeParam TType - Type of Prismic documents to update.
|
|
797
|
+
*
|
|
798
|
+
* @param id - The ID of the document to update.
|
|
799
|
+
* @param document - The document content to update.
|
|
800
|
+
* @param params - Additional fetch parameters.
|
|
801
|
+
*
|
|
802
|
+
* @see Prismic Migration API technical reference: {@link https://prismic.io/docs/migration-api-technical-reference}
|
|
803
|
+
*/
|
|
804
|
+
private async updateDocument<TType extends TDocuments["type"]>(
|
|
805
|
+
id: string,
|
|
806
|
+
document: MigrationDocument<ExtractDocumentType<TDocuments, TType>> & {
|
|
807
|
+
documentTitle?: string
|
|
808
|
+
},
|
|
809
|
+
params?: FetchParams,
|
|
810
|
+
): Promise<void> {
|
|
811
|
+
const url = new URL(`documents/${id}`, this.migrationAPIEndpoint)
|
|
812
|
+
|
|
813
|
+
await this.fetch<PutDocumentResult>(
|
|
814
|
+
url.toString(),
|
|
815
|
+
this.buildMigrationAPIQueryParams({
|
|
816
|
+
method: "PUT",
|
|
817
|
+
body: {
|
|
818
|
+
title: document.documentTitle,
|
|
819
|
+
uid: document.uid || undefined,
|
|
820
|
+
tags: document.tags,
|
|
821
|
+
data: document.data,
|
|
822
|
+
},
|
|
823
|
+
params,
|
|
824
|
+
}),
|
|
825
|
+
)
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
/**
|
|
829
|
+
* Builds fetch parameters for the Asset API.
|
|
830
|
+
*
|
|
831
|
+
* @typeParam TBody - Type of the body to send in the fetch request.
|
|
832
|
+
*
|
|
833
|
+
* @param params - Method, body, and additional fetch parameters.
|
|
834
|
+
*
|
|
835
|
+
* @returns An object that can be fetched to interact with the Asset API.
|
|
836
|
+
*
|
|
837
|
+
* @see Prismic Asset API technical reference: {@link https://prismic.io/docs/asset-api-technical-reference}
|
|
838
|
+
*/
|
|
839
|
+
private buildAssetAPIQueryParams<TBody = FormData | Record<string, unknown>>({
|
|
840
|
+
method,
|
|
841
|
+
body,
|
|
842
|
+
params,
|
|
843
|
+
}: {
|
|
844
|
+
method?: string
|
|
845
|
+
body?: TBody
|
|
846
|
+
params?: FetchParams
|
|
847
|
+
}): FetchParams {
|
|
848
|
+
const headers: Record<string, string> = {
|
|
849
|
+
...params?.fetchOptions?.headers,
|
|
850
|
+
authorization: `Bearer ${this.writeToken}`,
|
|
851
|
+
repository: this.repositoryName,
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
let _body: FormData | string | undefined
|
|
855
|
+
if (body instanceof FormData) {
|
|
856
|
+
_body = body
|
|
857
|
+
} else if (body) {
|
|
858
|
+
_body = JSON.stringify(body)
|
|
859
|
+
headers["content-type"] = "application/json"
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
return {
|
|
863
|
+
...params,
|
|
864
|
+
fetchOptions: {
|
|
865
|
+
...params?.fetchOptions,
|
|
866
|
+
method,
|
|
867
|
+
body: _body,
|
|
868
|
+
headers,
|
|
869
|
+
},
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
/**
|
|
874
|
+
* Builds fetch parameters for the Migration API.
|
|
875
|
+
*
|
|
876
|
+
* @typeParam TBody - Type of the body to send in the fetch request.
|
|
877
|
+
*
|
|
878
|
+
* @param params - Method, body, and additional fetch options.
|
|
879
|
+
*
|
|
880
|
+
* @returns An object that can be fetched to interact with the Migration API.
|
|
881
|
+
*
|
|
882
|
+
* @see Prismic Migration API technical reference: {@link https://prismic.io/docs/migration-api-technical-reference}
|
|
883
|
+
*/
|
|
884
|
+
private buildMigrationAPIQueryParams<
|
|
885
|
+
TBody extends PostDocumentParams | PutDocumentParams,
|
|
886
|
+
>({
|
|
887
|
+
method,
|
|
888
|
+
body,
|
|
889
|
+
params,
|
|
890
|
+
}: {
|
|
891
|
+
method?: string
|
|
892
|
+
body: TBody
|
|
893
|
+
params?: FetchParams
|
|
894
|
+
}): FetchParams {
|
|
895
|
+
return {
|
|
896
|
+
...params,
|
|
897
|
+
fetchOptions: {
|
|
898
|
+
...params?.fetchOptions,
|
|
899
|
+
method,
|
|
900
|
+
body: JSON.stringify(body),
|
|
901
|
+
headers: {
|
|
902
|
+
...params?.fetchOptions?.headers,
|
|
903
|
+
"content-type": "application/json",
|
|
904
|
+
repository: this.repositoryName,
|
|
905
|
+
authorization: `Bearer ${this.writeToken}`,
|
|
906
|
+
"x-api-key": this.migrationAPIKey,
|
|
907
|
+
},
|
|
908
|
+
},
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
}
|