@prismicio/client 7.10.0-alpha.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/dist/types/model/link.cjs.map +1 -1
- package/dist/types/model/link.d.ts +0 -1
- package/dist/types/model/link.js.map +1 -1
- package/dist/types/model/linkToMedia.d.ts +0 -1
- package/dist/types/value/contentRelationship.d.ts +0 -1
- package/dist/types/value/link.cjs.map +1 -1
- package/dist/types/value/link.d.ts +0 -2
- package/dist/types/value/link.js.map +1 -1
- package/dist/types/value/linkToMedia.d.ts +0 -1
- 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
- package/src/types/model/link.ts +0 -1
- package/src/types/model/linkToMedia.ts +0 -1
- package/src/types/value/contentRelationship.ts +0 -1
- package/src/types/value/link.ts +0 -2
- package/src/types/value/linkToMedia.ts +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prismicio/client",
|
|
3
|
-
"version": "7.10.0
|
|
3
|
+
"version": "7.10.0",
|
|
4
4
|
"description": "The official JavaScript + TypeScript client library for Prismic",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -82,7 +82,7 @@
|
|
|
82
82
|
"imgix-url-builder": "^0.0.5"
|
|
83
83
|
},
|
|
84
84
|
"devDependencies": {
|
|
85
|
-
"@prismicio/mock": "^0.3.
|
|
85
|
+
"@prismicio/mock": "^0.3.9",
|
|
86
86
|
"@prismicio/types-internal": "^2.6.0",
|
|
87
87
|
"@size-limit/preset-small-lib": "^11.1.4",
|
|
88
88
|
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
|
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
import { type LimitFunction, pLimit } from "./lib/pLimit"
|
|
2
|
+
|
|
3
|
+
import { PrismicError } from "./errors/PrismicError"
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The default delay used with APIs not providing rate limit headers.
|
|
7
|
+
*
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
export const UNKNOWN_RATE_LIMIT_DELAY = 1500
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* A universal API to make network requests. A subset of the `fetch()` API.
|
|
14
|
+
*
|
|
15
|
+
* {@link https://developer.mozilla.org/en-US/docs/Web/API/fetch}
|
|
16
|
+
*/
|
|
17
|
+
export type FetchLike = (
|
|
18
|
+
input: string,
|
|
19
|
+
init?: RequestInitLike,
|
|
20
|
+
) => Promise<ResponseLike>
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* An object that allows you to abort a `fetch()` request if needed via an
|
|
24
|
+
* `AbortController` object
|
|
25
|
+
*
|
|
26
|
+
* {@link https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal}
|
|
27
|
+
*/
|
|
28
|
+
// `any` is used often here to ensure this type is universally valid among
|
|
29
|
+
// different AbortSignal implementations. The types of each property are not
|
|
30
|
+
// important to validate since it is blindly passed to a given `fetch()`
|
|
31
|
+
// function.
|
|
32
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
33
|
+
export type AbortSignalLike = any
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* A subset of RequestInit properties to configure a `fetch()` request.
|
|
37
|
+
*/
|
|
38
|
+
// Only options relevant to the client are included. Extending from the full
|
|
39
|
+
// RequestInit would cause issues, such as accepting Header objects.
|
|
40
|
+
//
|
|
41
|
+
// An interface is used to allow other libraries to augment the type with
|
|
42
|
+
// environment-specific types.
|
|
43
|
+
export interface RequestInitLike extends Pick<RequestInit, "cache"> {
|
|
44
|
+
/**
|
|
45
|
+
* The HTTP method to use for the request.
|
|
46
|
+
*/
|
|
47
|
+
method?: string
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The request body to send with the request.
|
|
51
|
+
*/
|
|
52
|
+
// We want to keep the body type as compatible as possible, so
|
|
53
|
+
// we only declare the type we need and accept anything else.
|
|
54
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
55
|
+
body?: any | FormData | string
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* An object literal to set the `fetch()` request's headers.
|
|
59
|
+
*/
|
|
60
|
+
headers?: Record<string, string>
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* An AbortSignal to set the `fetch()` request's signal.
|
|
64
|
+
*
|
|
65
|
+
* See:
|
|
66
|
+
* [https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)
|
|
67
|
+
*/
|
|
68
|
+
// NOTE: `AbortSignalLike` is `any`! It is left as `AbortSignalLike`
|
|
69
|
+
// for backwards compatibility (the type is exported) and to signal to
|
|
70
|
+
// other readers that this should be an AbortSignal-like object.
|
|
71
|
+
signal?: AbortSignalLike
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* The minimum required properties from Response.
|
|
76
|
+
*/
|
|
77
|
+
export interface ResponseLike {
|
|
78
|
+
ok: boolean
|
|
79
|
+
status: number
|
|
80
|
+
headers: HeadersLike
|
|
81
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
82
|
+
json(): Promise<any>
|
|
83
|
+
text(): Promise<string>
|
|
84
|
+
blob(): Promise<Blob>
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* The minimum required properties from Headers.
|
|
89
|
+
*/
|
|
90
|
+
export interface HeadersLike {
|
|
91
|
+
get(name: string): string | null
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* The minimum required properties to treat as an HTTP Request for automatic
|
|
96
|
+
* Prismic preview support.
|
|
97
|
+
*/
|
|
98
|
+
export type HttpRequestLike =
|
|
99
|
+
| /**
|
|
100
|
+
* Web API Request
|
|
101
|
+
*
|
|
102
|
+
* @see http://developer.mozilla.org/en-US/docs/Web/API/Request
|
|
103
|
+
*/
|
|
104
|
+
{
|
|
105
|
+
headers?: {
|
|
106
|
+
get(name: string): string | null
|
|
107
|
+
}
|
|
108
|
+
url?: string
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Express-style Request
|
|
113
|
+
*/
|
|
114
|
+
| {
|
|
115
|
+
headers?: {
|
|
116
|
+
cookie?: string
|
|
117
|
+
}
|
|
118
|
+
query?: Record<string, unknown>
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Configuration for clients that determine how APIs are queried.
|
|
123
|
+
*/
|
|
124
|
+
export type BaseClientConfig = {
|
|
125
|
+
/**
|
|
126
|
+
* The function used to make network requests to the Prismic REST API. In
|
|
127
|
+
* environments where a global `fetch` function does not exist, such as
|
|
128
|
+
* Node.js, this function must be provided.
|
|
129
|
+
*/
|
|
130
|
+
fetch?: FetchLike
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Options provided to the client's `fetch()` on all network requests. These
|
|
134
|
+
* options will be merged with internally required options. They can also be
|
|
135
|
+
* overriden on a per-query basis using the query's `fetchOptions` parameter.
|
|
136
|
+
*/
|
|
137
|
+
fetchOptions?: RequestInitLike
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Parameters for any client method that use `fetch()`.
|
|
142
|
+
*/
|
|
143
|
+
export type FetchParams = {
|
|
144
|
+
/**
|
|
145
|
+
* Options provided to the client's `fetch()` on all network requests. These
|
|
146
|
+
* options will be merged with internally required options. They can also be
|
|
147
|
+
* overriden on a per-query basis using the query's `fetchOptions` parameter.
|
|
148
|
+
*/
|
|
149
|
+
fetchOptions?: RequestInitLike
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* An `AbortSignal` provided by an `AbortController`. This allows the network
|
|
153
|
+
* request to be cancelled if necessary.
|
|
154
|
+
*
|
|
155
|
+
* @deprecated Move the `signal` parameter into `fetchOptions.signal`:
|
|
156
|
+
*
|
|
157
|
+
* @see \<https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal\>
|
|
158
|
+
*/
|
|
159
|
+
signal?: AbortSignalLike
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* The result of a `fetch()` job.
|
|
164
|
+
*/
|
|
165
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
166
|
+
type FetchJobResult<TJSON = any> = {
|
|
167
|
+
status: number
|
|
168
|
+
headers: HeadersLike
|
|
169
|
+
json: TJSON
|
|
170
|
+
text?: string
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export abstract class BaseClient {
|
|
174
|
+
/**
|
|
175
|
+
* The function used to make network requests to the Prismic REST API. In
|
|
176
|
+
* environments where a global `fetch` function does not exist, such as
|
|
177
|
+
* Node.js, this function must be provided.
|
|
178
|
+
*/
|
|
179
|
+
fetchFn: FetchLike
|
|
180
|
+
|
|
181
|
+
fetchOptions?: RequestInitLike
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Active queued `fetch()` jobs keyed by URL and AbortSignal (if it exists).
|
|
185
|
+
*/
|
|
186
|
+
private queuedFetchJobs: Record<string, LimitFunction> = {}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Active deduped `fetch()` jobs keyed by URL and AbortSignal (if it exists).
|
|
190
|
+
*/
|
|
191
|
+
private dedupedFetchJobs: Record<
|
|
192
|
+
string,
|
|
193
|
+
Map<AbortSignalLike | undefined, Promise<FetchJobResult>>
|
|
194
|
+
> = {}
|
|
195
|
+
|
|
196
|
+
constructor(options: BaseClientConfig) {
|
|
197
|
+
this.fetchOptions = options.fetchOptions
|
|
198
|
+
|
|
199
|
+
if (typeof options.fetch === "function") {
|
|
200
|
+
this.fetchFn = options.fetch
|
|
201
|
+
} else if (typeof globalThis.fetch === "function") {
|
|
202
|
+
this.fetchFn = globalThis.fetch as FetchLike
|
|
203
|
+
} else {
|
|
204
|
+
throw new PrismicError(
|
|
205
|
+
"A valid fetch implementation was not provided. In environments where fetch is not available (including Node.js), a fetch implementation must be provided via a polyfill or the `fetch` option.",
|
|
206
|
+
undefined,
|
|
207
|
+
undefined,
|
|
208
|
+
)
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// If the global fetch function is used, we must bind it to the global scope.
|
|
212
|
+
if (this.fetchFn === globalThis.fetch) {
|
|
213
|
+
this.fetchFn = this.fetchFn.bind(globalThis)
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
protected async fetch(
|
|
218
|
+
url: string,
|
|
219
|
+
params: FetchParams = {},
|
|
220
|
+
): Promise<FetchJobResult> {
|
|
221
|
+
const requestInit: RequestInitLike = {
|
|
222
|
+
...this.fetchOptions,
|
|
223
|
+
...params.fetchOptions,
|
|
224
|
+
headers: {
|
|
225
|
+
...this.fetchOptions?.headers,
|
|
226
|
+
...params.fetchOptions?.headers,
|
|
227
|
+
},
|
|
228
|
+
signal:
|
|
229
|
+
params.fetchOptions?.signal ||
|
|
230
|
+
params.signal ||
|
|
231
|
+
this.fetchOptions?.signal,
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// Request with a `body` are throttled, others are deduped.
|
|
235
|
+
if (params.fetchOptions?.body) {
|
|
236
|
+
return this.queueFetch(url, requestInit)
|
|
237
|
+
} else {
|
|
238
|
+
return this.dedupeFetch(url, requestInit)
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
private queueFetch(
|
|
243
|
+
url: string,
|
|
244
|
+
requestInit: RequestInitLike = {},
|
|
245
|
+
): Promise<FetchJobResult> {
|
|
246
|
+
// Rate limiting is done per hostname.
|
|
247
|
+
const hostname = new URL(url).hostname
|
|
248
|
+
|
|
249
|
+
if (!this.queuedFetchJobs[hostname]) {
|
|
250
|
+
this.queuedFetchJobs[hostname] = pLimit({
|
|
251
|
+
interval: UNKNOWN_RATE_LIMIT_DELAY,
|
|
252
|
+
})
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
return this.queuedFetchJobs[hostname](() =>
|
|
256
|
+
this.createFetchJob(url, requestInit),
|
|
257
|
+
)
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
private dedupeFetch(
|
|
261
|
+
url: string,
|
|
262
|
+
requestInit: RequestInitLike = {},
|
|
263
|
+
): Promise<FetchJobResult> {
|
|
264
|
+
let job: Promise<FetchJobResult>
|
|
265
|
+
|
|
266
|
+
// `fetchJobs` is keyed twice: first by the URL and again by is
|
|
267
|
+
// signal, if one exists.
|
|
268
|
+
//
|
|
269
|
+
// Using two keys allows us to reuse fetch requests for
|
|
270
|
+
// equivalent URLs, but eject when we detect unique signals.
|
|
271
|
+
if (
|
|
272
|
+
this.dedupedFetchJobs[url] &&
|
|
273
|
+
this.dedupedFetchJobs[url].has(requestInit.signal)
|
|
274
|
+
) {
|
|
275
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
276
|
+
job = this.dedupedFetchJobs[url].get(requestInit.signal)!
|
|
277
|
+
} else {
|
|
278
|
+
this.dedupedFetchJobs[url] = this.dedupedFetchJobs[url] || new Map()
|
|
279
|
+
|
|
280
|
+
job = this.createFetchJob(url, requestInit).finally(() => {
|
|
281
|
+
this.dedupedFetchJobs[url]?.delete(requestInit.signal)
|
|
282
|
+
|
|
283
|
+
if (this.dedupedFetchJobs[url]?.size === 0) {
|
|
284
|
+
delete this.dedupedFetchJobs[url]
|
|
285
|
+
}
|
|
286
|
+
})
|
|
287
|
+
|
|
288
|
+
this.dedupedFetchJobs[url].set(requestInit.signal, job)
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
return job
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
private createFetchJob(
|
|
295
|
+
url: string,
|
|
296
|
+
requestInit: RequestInitLike = {},
|
|
297
|
+
): Promise<FetchJobResult> {
|
|
298
|
+
return this.fetchFn(url, requestInit).then(async (res) => {
|
|
299
|
+
// We can assume Prismic REST API responses
|
|
300
|
+
// will have a `application/json`
|
|
301
|
+
// Content Type. If not, this will
|
|
302
|
+
// throw, signaling an invalid
|
|
303
|
+
// response.
|
|
304
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
305
|
+
let json: any = undefined
|
|
306
|
+
let text: string | undefined = undefined
|
|
307
|
+
if (res.ok) {
|
|
308
|
+
try {
|
|
309
|
+
json = await res.json()
|
|
310
|
+
} catch {
|
|
311
|
+
// noop
|
|
312
|
+
}
|
|
313
|
+
} else {
|
|
314
|
+
try {
|
|
315
|
+
text = await res.text()
|
|
316
|
+
json = JSON.parse(text)
|
|
317
|
+
} catch {
|
|
318
|
+
// noop
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
return {
|
|
323
|
+
status: res.status,
|
|
324
|
+
headers: res.headers,
|
|
325
|
+
json,
|
|
326
|
+
text,
|
|
327
|
+
}
|
|
328
|
+
})
|
|
329
|
+
}
|
|
330
|
+
}
|