@prismicio/client 6.0.0 → 6.1.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/src/client.ts CHANGED
@@ -347,6 +347,8 @@ export class Client {
347
347
  if (this.fetchFn === globalThis.fetch) {
348
348
  this.fetchFn = this.fetchFn.bind(globalThis);
349
349
  }
350
+
351
+ this.graphqlFetch = this.graphqlFetch.bind(this);
350
352
  }
351
353
 
352
354
  /**
@@ -1165,6 +1167,7 @@ export class Client {
1165
1167
  if (documentID != null) {
1166
1168
  const document = await this.getByID(documentID, {
1167
1169
  ref: previewToken,
1170
+ lang: "*",
1168
1171
  });
1169
1172
 
1170
1173
  // We know we have a valid field to resolve since we are using prismicH.documentToLinkField
@@ -1261,6 +1264,70 @@ export class Client {
1261
1264
  };
1262
1265
  }
1263
1266
 
1267
+ /**
1268
+ * A `fetch()` function to be used with GraphQL clients configured for
1269
+ * Prismic's GraphQL API. It automatically applies the necessary `prismic-ref`
1270
+ * and Authorization headers.
1271
+ *
1272
+ * @example
1273
+ *
1274
+ * ```ts
1275
+ * const graphqlClient = new ApolloClient({
1276
+ * link: new HttpLink({
1277
+ * uri: prismic.getGraphQLEndpoint(repositoryName),
1278
+ * // Provide `client.graphqlFetch` as the fetch implementation.
1279
+ * fetch: client.graphqlFetch,
1280
+ * // Using GET is required.
1281
+ * useGETForQueries: true,
1282
+ * }),
1283
+ * cache: new InMemoryCache(),
1284
+ * });
1285
+ * ```
1286
+ *
1287
+ * @param input - The `fetch()` `input` parameter. Only strings are supported.
1288
+ * @param init - The `fetch()` `init` parameter. Only plain objects are supported.
1289
+ *
1290
+ * @returns The `fetch()` Response for the request.
1291
+ * @experimental
1292
+ */
1293
+ async graphqlFetch(
1294
+ input: RequestInfo,
1295
+ init?: RequestInit,
1296
+ ): Promise<Response> {
1297
+ const cachedRepository = await this.getCachedRepository();
1298
+ const ref = await this.getResolvedRefString();
1299
+
1300
+ const unsanitizedHeaders: Record<string, string> = {
1301
+ "Prismic-ref": ref,
1302
+ "Prismic-integration-field-ref":
1303
+ cachedRepository.integrationFieldsRef || "",
1304
+ Authorization: this.accessToken ? `Token ${this.accessToken}` : "",
1305
+ // Asserting `init.headers` is a Record since popular GraphQL
1306
+ // libraries pass this as a Record. Header objects as input
1307
+ // are unsupported.
1308
+ ...(init ? (init.headers as Record<string, string>) : {}),
1309
+ };
1310
+
1311
+ const headers: Record<string, string> = {};
1312
+ for (const key in unsanitizedHeaders) {
1313
+ if (unsanitizedHeaders[key]) {
1314
+ headers[key.toLowerCase()] =
1315
+ unsanitizedHeaders[key as keyof typeof unsanitizedHeaders];
1316
+ }
1317
+ }
1318
+
1319
+ return (await this.fetchFn(
1320
+ // Asserting `input` is a string since popular GraphQL
1321
+ // libraries pass this as a string. Request objects as
1322
+ // input are unsupported.
1323
+ input as string,
1324
+ {
1325
+ ...init,
1326
+ headers,
1327
+ },
1328
+ )) as Response;
1329
+ }
1330
+
1264
1331
  /**
1265
1332
  * Returns a cached version of `getRepository` with a TTL.
1266
1333
  *
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Get a repository's Prismic GraphQL endpoint.
3
+ *
4
+ * @typeParam RepositoryName - Name of the Prismic repository.
5
+ * @param repositoryName - Name of the repository.
6
+ *
7
+ * @returns The repository's Prismic REST API V2 endpoint
8
+ */
9
+ export const getGraphQLEndpoint = <RepositoryName extends string>(
10
+ repositoryName: RepositoryName,
11
+ ): `https://${RepositoryName}.cdn.prismic.io/graphql` =>
12
+ `https://${repositoryName}.cdn.prismic.io/graphql` as const;
package/src/index.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  // Primary library API.
2
2
  export * from "./getEndpoint";
3
+ export * from "./getGraphQLEndpoint";
3
4
  export * from "./buildQueryURL";
4
5
  export { createClient, Client } from "./client";
5
6