@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.
Files changed (121) hide show
  1. package/dist/BaseClient.cjs +108 -0
  2. package/dist/BaseClient.cjs.map +1 -0
  3. package/dist/BaseClient.d.ts +152 -0
  4. package/dist/BaseClient.js +108 -0
  5. package/dist/BaseClient.js.map +1 -0
  6. package/dist/Client.cjs +1217 -0
  7. package/dist/Client.cjs.map +1 -0
  8. package/dist/Client.d.ts +924 -0
  9. package/dist/Client.js +1217 -0
  10. package/dist/Client.js.map +1 -0
  11. package/dist/Migration.cjs +318 -0
  12. package/dist/Migration.cjs.map +1 -0
  13. package/dist/Migration.d.ts +213 -0
  14. package/dist/Migration.js +318 -0
  15. package/dist/Migration.js.map +1 -0
  16. package/dist/WriteClient.cjs +512 -0
  17. package/dist/WriteClient.cjs.map +1 -0
  18. package/dist/WriteClient.d.ts +314 -0
  19. package/dist/WriteClient.js +512 -0
  20. package/dist/WriteClient.js.map +1 -0
  21. package/dist/createClient.cjs +2 -1215
  22. package/dist/createClient.cjs.map +1 -1
  23. package/dist/createClient.d.ts +2 -1003
  24. package/dist/createClient.js +1 -1214
  25. package/dist/createClient.js.map +1 -1
  26. package/dist/createMigration.cjs +6 -0
  27. package/dist/createMigration.cjs.map +1 -0
  28. package/dist/createMigration.d.ts +24 -0
  29. package/dist/createMigration.js +6 -0
  30. package/dist/createMigration.js.map +1 -0
  31. package/dist/createWriteClient.cjs +6 -0
  32. package/dist/createWriteClient.cjs.map +1 -0
  33. package/dist/createWriteClient.d.ts +32 -0
  34. package/dist/createWriteClient.js +6 -0
  35. package/dist/createWriteClient.js.map +1 -0
  36. package/dist/getRepositoryName.cjs +7 -2
  37. package/dist/getRepositoryName.cjs.map +1 -1
  38. package/dist/getRepositoryName.d.ts +3 -6
  39. package/dist/getRepositoryName.js +7 -2
  40. package/dist/getRepositoryName.js.map +1 -1
  41. package/dist/index.cjs +14 -1
  42. package/dist/index.cjs.map +1 -1
  43. package/dist/index.d.ts +17 -2
  44. package/dist/index.js +14 -1
  45. package/dist/index.js.map +1 -1
  46. package/dist/lib/isMigrationValue.cjs +24 -0
  47. package/dist/lib/isMigrationValue.cjs.map +1 -0
  48. package/dist/lib/isMigrationValue.d.ts +62 -0
  49. package/dist/lib/isMigrationValue.js +24 -0
  50. package/dist/lib/isMigrationValue.js.map +1 -0
  51. package/dist/lib/isValue.cjs +53 -0
  52. package/dist/lib/isValue.cjs.map +1 -0
  53. package/dist/lib/isValue.d.ts +72 -0
  54. package/dist/lib/isValue.js +53 -0
  55. package/dist/lib/isValue.js.map +1 -0
  56. package/dist/lib/pLimit.cjs +49 -0
  57. package/dist/lib/pLimit.cjs.map +1 -0
  58. package/dist/lib/pLimit.d.ts +22 -0
  59. package/dist/lib/pLimit.js +49 -0
  60. package/dist/lib/pLimit.js.map +1 -0
  61. package/dist/lib/resolveMigrationDocumentData.cjs +123 -0
  62. package/dist/lib/resolveMigrationDocumentData.cjs.map +1 -0
  63. package/dist/lib/resolveMigrationDocumentData.d.ts +52 -0
  64. package/dist/lib/resolveMigrationDocumentData.js +123 -0
  65. package/dist/lib/resolveMigrationDocumentData.js.map +1 -0
  66. package/dist/lib/validateAssetMetadata.cjs +28 -0
  67. package/dist/lib/validateAssetMetadata.cjs.map +1 -0
  68. package/dist/lib/validateAssetMetadata.d.ts +10 -0
  69. package/dist/lib/validateAssetMetadata.js +28 -0
  70. package/dist/lib/validateAssetMetadata.js.map +1 -0
  71. package/dist/package.json.cjs +1 -1
  72. package/dist/package.json.js +1 -1
  73. package/dist/types/api/asset/asset.d.ts +177 -0
  74. package/dist/types/api/asset/tag.d.ts +53 -0
  75. package/dist/types/api/migration/document.d.ts +60 -0
  76. package/dist/types/migration/Asset.cjs +33 -0
  77. package/dist/types/migration/Asset.cjs.map +1 -0
  78. package/dist/types/migration/Asset.d.ts +109 -0
  79. package/dist/types/migration/Asset.js +33 -0
  80. package/dist/types/migration/Asset.js.map +1 -0
  81. package/dist/types/migration/ContentRelationship.d.ts +17 -0
  82. package/dist/types/migration/Document.cjs +47 -0
  83. package/dist/types/migration/Document.cjs.map +1 -0
  84. package/dist/types/migration/Document.d.ts +94 -0
  85. package/dist/types/migration/Document.js +47 -0
  86. package/dist/types/migration/Document.js.map +1 -0
  87. package/dist/types/model/link.cjs.map +1 -1
  88. package/dist/types/model/link.d.ts +0 -1
  89. package/dist/types/model/link.js.map +1 -1
  90. package/dist/types/model/linkToMedia.d.ts +0 -1
  91. package/dist/types/value/contentRelationship.d.ts +0 -1
  92. package/dist/types/value/link.cjs.map +1 -1
  93. package/dist/types/value/link.d.ts +0 -2
  94. package/dist/types/value/link.js.map +1 -1
  95. package/dist/types/value/linkToMedia.d.ts +0 -1
  96. package/package.json +2 -2
  97. package/src/BaseClient.ts +330 -0
  98. package/src/Client.ts +1787 -0
  99. package/src/Migration.ts +531 -0
  100. package/src/WriteClient.ts +911 -0
  101. package/src/createClient.ts +3 -1931
  102. package/src/createMigration.ts +31 -0
  103. package/src/createWriteClient.ts +43 -0
  104. package/src/getRepositoryName.ts +19 -14
  105. package/src/index.ts +32 -4
  106. package/src/lib/isMigrationValue.ts +126 -0
  107. package/src/lib/isValue.ts +196 -0
  108. package/src/lib/pLimit.ts +106 -0
  109. package/src/lib/resolveMigrationDocumentData.ts +233 -0
  110. package/src/lib/validateAssetMetadata.ts +82 -0
  111. package/src/types/api/asset/asset.ts +211 -0
  112. package/src/types/api/asset/tag.ts +59 -0
  113. package/src/types/api/migration/document.ts +84 -0
  114. package/src/types/migration/Asset.ts +156 -0
  115. package/src/types/migration/ContentRelationship.ts +36 -0
  116. package/src/types/migration/Document.ts +157 -0
  117. package/src/types/model/link.ts +0 -1
  118. package/src/types/model/linkToMedia.ts +0 -1
  119. package/src/types/value/contentRelationship.ts +0 -1
  120. package/src/types/value/link.ts +0 -2
  121. package/src/types/value/linkToMedia.ts +0 -1
@@ -0,0 +1,31 @@
1
+ import type { PrismicDocument } from "./types/value/document"
2
+
3
+ import { Migration } from "./Migration"
4
+
5
+ /**
6
+ * Type definitions for the `createMigration()` function. May be augmented by
7
+ * third-party libraries.
8
+ */
9
+ export interface CreateMigration {
10
+ <TDocuments extends PrismicDocument>(
11
+ ...args: ConstructorParameters<typeof Migration>
12
+ ): Migration<TDocuments>
13
+ }
14
+
15
+ /**
16
+ * Creates a Prismic migration instance that can be used to prepare your
17
+ * migration to Prismic.
18
+ *
19
+ * @example
20
+ *
21
+ * ```ts
22
+ * createMigration()
23
+ * ```
24
+ *
25
+ * @typeParam TDocuments - A union of Prismic document types for the repository.
26
+ *
27
+ * @returns A migration instance to prepare your migration.
28
+ */
29
+ export const createMigration: CreateMigration = <
30
+ TDocuments extends PrismicDocument,
31
+ >() => new Migration<TDocuments>()
@@ -0,0 +1,43 @@
1
+ import type { PrismicDocument } from "./types/value/document"
2
+
3
+ import type { WriteClientConfig } from "./WriteClient"
4
+ import { WriteClient } from "./WriteClient"
5
+
6
+ /**
7
+ * Type definitions for the `createWriteClient()` function. May be augmented by
8
+ * third-party libraries.
9
+ */
10
+ export interface CreateWriteClient {
11
+ <TDocuments extends PrismicDocument>(
12
+ ...args: ConstructorParameters<typeof WriteClient>
13
+ ): WriteClient<TDocuments>
14
+ }
15
+
16
+ /**
17
+ * Creates a Prismic client that can be used to query and write content to a
18
+ * repository.
19
+ *
20
+ * @remarks
21
+ * This client works in environments supporting File, Blob, and FormData,
22
+ * including Node.js 20 and later.
23
+ *
24
+ * @example
25
+ *
26
+ * ```ts
27
+ * createWriteClient("qwerty", { writeToken: "***" })
28
+ * ```
29
+ *
30
+ * @typeParam TDocuments - A union of Prismic document types for the repository.
31
+ *
32
+ * @param repositoryName - The Prismic repository name for the repository.
33
+ * @param options - Configuration that determines how content will be queried
34
+ * from and written to the Prismic repository.
35
+ *
36
+ * @returns A client that can query and write content to the repository.
37
+ */
38
+ export const createWriteClient: CreateWriteClient = <
39
+ TDocuments extends PrismicDocument,
40
+ >(
41
+ repositoryName: string,
42
+ options: WriteClientConfig,
43
+ ) => new WriteClient<TDocuments>(repositoryName, options)
@@ -1,27 +1,32 @@
1
1
  import { PrismicError } from "./errors/PrismicError"
2
2
 
3
3
  /**
4
- * Get a Prismic repository's name from its standard Prismic Rest API V2 or
4
+ * Get a Prismic repository's name from its standard Prismic Document API or
5
5
  * GraphQL endpoint.
6
6
  *
7
- * @typeParam RepositoryEndpoint - Prismic Rest API V2 endpoint for the
8
- * repository.
9
- *
10
- * @param repositoryEndpoint - Prismic Rest API V2 endpoint for the repository.
7
+ * @param repositoryEndpoint - Prismic Document API endpoint for the repository.
11
8
  *
12
9
  * @returns The Prismic repository's name.
13
10
  *
14
- * @throws {@link Error} Thrown if an invalid Prismic Rest API V2 endpoint is
11
+ * @throws {@link Error} Thrown if an invalid Prismic Document API endpoint is
15
12
  * provided.
16
13
  */
17
14
  export const getRepositoryName = (repositoryEndpoint: string): string => {
18
15
  try {
19
- return new URL(repositoryEndpoint).hostname.split(".")[0]
20
- } catch {
21
- throw new PrismicError(
22
- `An invalid Prismic Rest API V2 endpoint was provided: ${repositoryEndpoint}`,
23
- undefined,
24
- undefined,
25
- )
26
- }
16
+ const hostname = new URL(repositoryEndpoint).hostname
17
+
18
+ if (
19
+ hostname.endsWith("prismic.io") || // Production
20
+ hostname.endsWith("wroom.io") || // Staging
21
+ hostname.endsWith("wroom.test") // Dev
22
+ ) {
23
+ return hostname.split(".")[0]
24
+ }
25
+ } catch {}
26
+
27
+ throw new PrismicError(
28
+ `An invalid Prismic Document API endpoint was provided: ${repositoryEndpoint}`,
29
+ undefined,
30
+ undefined,
31
+ )
27
32
  }
package/src/index.ts CHANGED
@@ -16,7 +16,16 @@ import { filter } from "./filter"
16
16
  //=============================================================================
17
17
 
18
18
  // Primary Client API.
19
- export { createClient, Client } from "./createClient"
19
+ export { createClient } from "./createClient"
20
+ export { Client } from "./Client"
21
+
22
+ // Write Client API.
23
+ export { createWriteClient } from "./createWriteClient"
24
+ export { WriteClient } from "./WriteClient"
25
+
26
+ // Migration helper.
27
+ export { createMigration } from "./createMigration"
28
+ export { Migration } from "./Migration"
20
29
 
21
30
  // API endpoint helpers.
22
31
  export { getRepositoryEndpoint } from "./getRepositoryEndpoint"
@@ -41,21 +50,24 @@ export { filter, predicate }
41
50
  export * as cookie from "./cookie"
42
51
 
43
52
  // General types used to query content from Prismic. These are made public to allow users to better type their projects.
53
+ export type { CreateClient } from "./createClient"
54
+ export type { ClientConfig } from "./Client"
55
+ export type { CreateWriteClient } from "./createWriteClient"
56
+ export type { WriteClientConfig, MigrateReporterEvents } from "./WriteClient"
44
57
  export type {
45
58
  AbortSignalLike,
46
- ClientConfig,
47
- CreateClient,
48
59
  FetchLike,
49
60
  HttpRequestLike,
50
61
  RequestInitLike,
51
62
  ResponseLike,
52
- } from "./createClient"
63
+ } from "./BaseClient"
53
64
  export type {
54
65
  BuildQueryURLArgs,
55
66
  Ordering,
56
67
  QueryParams,
57
68
  Route,
58
69
  } from "./buildQueryURL"
70
+ export type { CreateMigration } from "./createMigration"
59
71
 
60
72
  //=============================================================================
61
73
  // Helpers - Manipulate content from Prismic.
@@ -313,6 +325,22 @@ export type {
313
325
  CustomTypeModelFieldForSlicePrimary,
314
326
  } from "./types/model/types"
315
327
 
328
+ // Migrations - Types representing Prismic Migration API content values.
329
+ export { PrismicMigrationDocument } from "./types/migration/Document"
330
+ export type {
331
+ PendingPrismicDocument,
332
+ ExistingPrismicDocument,
333
+ InjectMigrationSpecificTypes,
334
+ } from "./types/migration/Document"
335
+
336
+ export { PrismicMigrationAsset } from "./types/migration/Asset"
337
+ export type {
338
+ MigrationImage,
339
+ MigrationLinkToMedia,
340
+ MigrationRTImageNode,
341
+ } from "./types/migration/Asset"
342
+ export type { MigrationContentRelationship } from "./types/migration/ContentRelationship"
343
+
316
344
  // API - Types representing Prismic Rest API V2 responses.
317
345
  export type { Query } from "./types/api/query"
318
346
 
@@ -0,0 +1,126 @@
1
+ import type {
2
+ MigrationImage,
3
+ MigrationLinkToMedia,
4
+ MigrationRTImageNode,
5
+ } from "../types/migration/Asset"
6
+ import { PrismicMigrationAsset } from "../types/migration/Asset"
7
+ import type { MigrationContentRelationship } from "../types/migration/ContentRelationship"
8
+ import {
9
+ type InjectMigrationSpecificTypes,
10
+ PrismicMigrationDocument,
11
+ } from "../types/migration/Document"
12
+ import type { PrismicDocument } from "../types/value/document"
13
+ import type { GroupField } from "../types/value/group"
14
+ import { LinkType } from "../types/value/link"
15
+ import { RichTextNodeType } from "../types/value/richText"
16
+ import type { SliceZone } from "../types/value/sliceZone"
17
+ import type { AnyRegularField } from "../types/value/types"
18
+
19
+ import * as is from "./isValue"
20
+
21
+ /**
22
+ * Unknown value to check if it's a specific field type.
23
+ *
24
+ * @remarks
25
+ * Explicit types are added to help ensure narrowing is done effectively.
26
+ */
27
+ type UnknownValue =
28
+ | PrismicDocument
29
+ | InjectMigrationSpecificTypes<AnyRegularField | GroupField | SliceZone>
30
+ | unknown
31
+
32
+ /**
33
+ * Checks if a value is a migration content relationship field.
34
+ *
35
+ * @param value - Value to check.
36
+ *
37
+ * @returns `true` if `value` is a migration content relationship field, `false`
38
+ * otherwise.
39
+ *
40
+ * @internal
41
+ * This is not an official helper function and it's only designed to work with internal processes.
42
+ */
43
+ export const contentRelationship = (
44
+ value: UnknownValue,
45
+ ): value is MigrationContentRelationship => {
46
+ return (
47
+ value instanceof PrismicMigrationDocument ||
48
+ is.prismicDocument(value) ||
49
+ (typeof value === "object" &&
50
+ value !== null &&
51
+ "link_type" in value &&
52
+ value.link_type === LinkType.Document &&
53
+ "id" in value &&
54
+ (contentRelationship(value.id) || typeof value.id === "function"))
55
+ )
56
+ }
57
+
58
+ /**
59
+ * Checks if a value is a migration image field.
60
+ *
61
+ * @param value - Value to check.
62
+ *
63
+ * @returns `true` if `value` is a migration image field, `false` otherwise.
64
+ *
65
+ * @internal
66
+ * This is not an official helper function and it's only designed to work with internal processes.
67
+ */
68
+ export const image = (value: UnknownValue): value is MigrationImage => {
69
+ return (
70
+ value instanceof PrismicMigrationAsset ||
71
+ (typeof value === "object" &&
72
+ value !== null &&
73
+ "id" in value &&
74
+ Object.values(value).every(
75
+ (maybeThumbnail) => maybeThumbnail instanceof PrismicMigrationAsset,
76
+ ))
77
+ )
78
+ }
79
+
80
+ /**
81
+ * Checks if a value is a migration link to media field.
82
+ *
83
+ * @param value - Value to check.
84
+ *
85
+ * @returns `true` if `value` is a migration link to media field, `false`
86
+ * otherwise.
87
+ *
88
+ * @internal
89
+ * This is not an official helper function and it's only designed to work with internal processes.
90
+ */
91
+ export const linkToMedia = (
92
+ value: UnknownValue,
93
+ ): value is MigrationLinkToMedia => {
94
+ return (
95
+ typeof value === "object" &&
96
+ value !== null &&
97
+ "id" in value &&
98
+ value.id instanceof PrismicMigrationAsset &&
99
+ "link_type" in value &&
100
+ value.link_type === LinkType.Media
101
+ )
102
+ }
103
+
104
+ /**
105
+ * Checks if a value is a migration rich text image node.
106
+ *
107
+ * @param value - Value to check.
108
+ *
109
+ * @returns `true` if `value` is a migration rich text image node, `false`
110
+ * otherwise.
111
+ *
112
+ * @internal
113
+ * This is not an official helper function and it's only designed to work with internal processes.
114
+ */
115
+ export const rtImageNode = (
116
+ value: UnknownValue,
117
+ ): value is MigrationRTImageNode => {
118
+ return (
119
+ typeof value === "object" &&
120
+ value !== null &&
121
+ "id" in value &&
122
+ value.id instanceof PrismicMigrationAsset &&
123
+ "type" in value &&
124
+ value.type === RichTextNodeType.image
125
+ )
126
+ }
@@ -0,0 +1,196 @@
1
+ import type { InjectMigrationSpecificTypes } from "../types/migration/Document"
2
+ import type { FilledContentRelationshipField } from "../types/value/contentRelationship"
3
+ import type { PrismicDocument } from "../types/value/document"
4
+ import type { GroupField } from "../types/value/group"
5
+ import type { ImageField } from "../types/value/image"
6
+ import { LinkType } from "../types/value/link"
7
+ import type { FilledLinkToMediaField } from "../types/value/linkToMedia"
8
+ import { type RTImageNode, RichTextNodeType } from "../types/value/richText"
9
+ import type { SliceZone } from "../types/value/sliceZone"
10
+ import type { AnyRegularField } from "../types/value/types"
11
+
12
+ /**
13
+ * Unknown value to check if it's a specific field type.
14
+ *
15
+ * @remarks
16
+ * Explicit types are added to help ensure narrowing is done effectively.
17
+ */
18
+ type UnknownValue =
19
+ | PrismicDocument
20
+ | InjectMigrationSpecificTypes<AnyRegularField | GroupField | SliceZone>
21
+ | unknown
22
+
23
+ /**
24
+ * Checks if a value is a link to media field.
25
+ *
26
+ * @param value - Value to check.
27
+ *
28
+ * @returns `true` if `value` is a link to media field, `false` otherwise.
29
+ *
30
+ * @internal
31
+ * This is not an official helper function and it's only designed to work with internal processes.
32
+ */
33
+ export const filledLinkToMedia = (
34
+ value: UnknownValue,
35
+ ): value is FilledLinkToMediaField => {
36
+ if (value && typeof value === "object" && !("version" in value)) {
37
+ if (
38
+ "link_type" in value &&
39
+ value.link_type === LinkType.Media &&
40
+ "id" in value &&
41
+ "name" in value &&
42
+ "kind" in value &&
43
+ "url" in value &&
44
+ "size" in value
45
+ ) {
46
+ value
47
+
48
+ return true
49
+ }
50
+ }
51
+
52
+ return false
53
+ }
54
+
55
+ /**
56
+ * Checks if a value is like an image field.
57
+ *
58
+ * @param value - Value to check.
59
+ *
60
+ * @returns `true` if `value` is like an image field, `false` otherwise.
61
+ *
62
+ * @internal
63
+ * This is not an official helper function and it's only designed to work with internal processes.
64
+ */
65
+ const imageLike = (
66
+ value: UnknownValue,
67
+ ): value is ImageField<string, "filled"> | RTImageNode => {
68
+ if (
69
+ value &&
70
+ typeof value === "object" &&
71
+ (!("version" in value) || typeof value.version === "object")
72
+ ) {
73
+ if (
74
+ "id" in value &&
75
+ "url" in value &&
76
+ typeof value.url === "string" &&
77
+ "dimensions" in value &&
78
+ "edit" in value &&
79
+ "alt" in value &&
80
+ "copyright" in value
81
+ ) {
82
+ return true
83
+ }
84
+ }
85
+
86
+ return false
87
+ }
88
+
89
+ /**
90
+ * Checks if a value is an image field.
91
+ *
92
+ * @param value - Value to check.
93
+ *
94
+ * @returns `true` if `value` is an image field, `false` otherwise.
95
+ *
96
+ * @internal
97
+ * This is not an official helper function and it's only designed to work with internal processes.
98
+ */
99
+ export const filledImage = (
100
+ value: UnknownValue,
101
+ ): value is ImageField<string, "filled"> => {
102
+ if (
103
+ imageLike(value) &&
104
+ (!("type" in value) || value.type !== RichTextNodeType.image)
105
+ ) {
106
+ value
107
+
108
+ return true
109
+ }
110
+
111
+ return false
112
+ }
113
+
114
+ /**
115
+ * Checks if a value is a rich text image node.
116
+ *
117
+ * @param value - Value to check.
118
+ *
119
+ * @returns `true` if `value` is a rich text image node, `false` otherwise.
120
+ *
121
+ * @internal
122
+ * This is not an official helper function and it's only designed to work with internal processes.
123
+ */
124
+ export const rtImageNode = (value: UnknownValue): value is RTImageNode => {
125
+ if (
126
+ imageLike(value) &&
127
+ "type" in value &&
128
+ value.type === RichTextNodeType.image
129
+ ) {
130
+ value
131
+
132
+ return true
133
+ }
134
+
135
+ return false
136
+ }
137
+
138
+ /**
139
+ * Checks if a value is a content relationship field.
140
+ *
141
+ * @param value - Value to check.
142
+ *
143
+ * @returns `true` if `value` is a content relationship, `false` otherwise.
144
+ *
145
+ * @internal
146
+ * This is not an official helper function and it's only designed to work with internal processes.
147
+ */
148
+ export const filledContentRelationship = (
149
+ value: UnknownValue,
150
+ ): value is FilledContentRelationshipField => {
151
+ if (value && typeof value === "object" && !("version" in value)) {
152
+ if (
153
+ "link_type" in value &&
154
+ value.link_type === LinkType.Document &&
155
+ "id" in value &&
156
+ "type" in value &&
157
+ "tags" in value &&
158
+ "lang" in value
159
+ ) {
160
+ return true
161
+ }
162
+ }
163
+
164
+ return false
165
+ }
166
+
167
+ /**
168
+ * Checks if a value is a Prismic document.
169
+ *
170
+ * @param value - Value to check.
171
+ *
172
+ * @returns `true` if `value` is a Prismic document, `false` otherwise.
173
+ *
174
+ * @internal
175
+ * This is not an official helper function and it's only designed to work with internal processes.
176
+ */
177
+ export const prismicDocument = (
178
+ value: UnknownValue,
179
+ ): value is PrismicDocument => {
180
+ try {
181
+ return (
182
+ typeof value === "object" &&
183
+ value !== null &&
184
+ "id" in value &&
185
+ "href" in value &&
186
+ typeof value.href === "string" &&
187
+ new URL(value.href) &&
188
+ "type" in value &&
189
+ "lang" in value &&
190
+ "tags" in value &&
191
+ Array.isArray(value.tags)
192
+ )
193
+ } catch {
194
+ return false
195
+ }
196
+ }
@@ -0,0 +1,106 @@
1
+ /*
2
+ ** Core logic from https://github.com/sindresorhus/p-limit
3
+ ** Many thanks to @sindresorhus
4
+ */
5
+
6
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
7
+ type AnyFunction = (...arguments_: readonly any[]) => unknown
8
+
9
+ const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
10
+
11
+ /**
12
+ * @param fn - Promise-returning/async function.
13
+ * @param arguments - Any arguments to pass through to `fn`. Support for passing
14
+ * arguments on to the `fn` is provided in order to be able to avoid creating
15
+ * unnecessary closures. You probably don't need this optimization unless
16
+ * you're pushing a lot of functions.
17
+ *
18
+ * @returns The promise returned by calling `fn(...arguments)`.
19
+ */
20
+ export type LimitFunction = <TArguments extends unknown[], TReturnType>(
21
+ function_: (
22
+ ...arguments_: TArguments
23
+ ) => PromiseLike<TReturnType> | TReturnType,
24
+ ...arguments_: TArguments
25
+ ) => Promise<TReturnType>
26
+
27
+ /**
28
+ * Creates a limiting function that will only execute one promise at a time and
29
+ * respect a given interval between each call.
30
+ *
31
+ * @param args - Options for the function, `interval` is the minimum time to
32
+ * wait between each promise execution.
33
+ *
34
+ * @returns A limiting function as per configuration, see {@link LimitFunction}.
35
+ */
36
+ export const pLimit = ({
37
+ interval,
38
+ }: { interval?: number } = {}): LimitFunction => {
39
+ const queue: AnyFunction[] = []
40
+ let busy = false
41
+ let lastCompletion = 0
42
+
43
+ const resumeNext = () => {
44
+ if (!busy && queue.length > 0) {
45
+ queue.shift()?.()
46
+ busy = true
47
+ }
48
+ }
49
+
50
+ const next = () => {
51
+ busy = false
52
+
53
+ resumeNext()
54
+ }
55
+
56
+ const run = async (
57
+ function_: AnyFunction,
58
+ resolve: (value: unknown) => void,
59
+ arguments_: unknown[],
60
+ ) => {
61
+ const timeSinceLastCompletion = Date.now() - lastCompletion
62
+
63
+ if (interval && timeSinceLastCompletion < interval) {
64
+ await sleep(interval - timeSinceLastCompletion)
65
+ }
66
+ const result = (async () => function_(...arguments_))()
67
+
68
+ resolve(result)
69
+
70
+ try {
71
+ await result
72
+ } catch {}
73
+
74
+ lastCompletion = Date.now()
75
+
76
+ next()
77
+ }
78
+
79
+ const enqueue = (
80
+ function_: AnyFunction,
81
+ resolve: (value: unknown) => void,
82
+ arguments_: unknown[],
83
+ ) => {
84
+ // Queue `internalResolve` instead of the `run` function
85
+ // to preserve asynchronous context.
86
+ new Promise((internalResolve) => {
87
+ queue.push(internalResolve)
88
+ }).then(run.bind(undefined, function_, resolve, arguments_))
89
+ ;(async () => {
90
+ // This function needs to wait until the next microtask before comparing
91
+ // `activeCount` to `concurrency`, because `activeCount` is updated asynchronously
92
+ // after the `internalResolve` function is dequeued and called. The comparison in the if-statement
93
+ // needs to happen asynchronously as well to get an up-to-date value for `activeCount`.
94
+ await Promise.resolve()
95
+
96
+ if (!busy) {
97
+ resumeNext()
98
+ }
99
+ })()
100
+ }
101
+
102
+ return ((function_: AnyFunction, ...arguments_: unknown[]) =>
103
+ new Promise<unknown>((resolve) => {
104
+ enqueue(function_, resolve, arguments_)
105
+ })) as LimitFunction
106
+ }