@prismicio/client 6.0.0-beta.4 → 6.1.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/client",
3
- "version": "6.0.0-beta.4",
3
+ "version": "6.1.1",
4
4
  "description": "The official JavaScript + TypeScript client library for Prismic",
5
5
  "keywords": [
6
6
  "prismic",
@@ -42,39 +42,43 @@
42
42
  "release:beta": "npm run build && npm run test && standard-version --release-as major --prerelease beta && git push --follow-tags && npm run build && npm publish --tag beta",
43
43
  "release:beta:dry": "standard-version --release-as major --prerelease beta --dry-run",
44
44
  "release:dry": "standard-version --dry-run",
45
- "test": "npm run lint && npm run unit",
45
+ "size": "size-limit",
46
+ "test": "npm run lint && npm run unit && npm run build && npm run size",
46
47
  "unit": "nyc --reporter=lcovonly --reporter=text --exclude-after-remap=false ava"
47
48
  },
48
49
  "dependencies": {
49
- "@prismicio/helpers": "^2.0.0-beta.6",
50
- "@prismicio/types": "^0.1.19"
50
+ "@prismicio/helpers": "^2.0.1",
51
+ "@prismicio/types": "^0.1.22"
51
52
  },
52
53
  "devDependencies": {
53
- "@types/node-fetch": "^2.5.12",
54
- "@types/sinon": "^10.0.6",
55
- "@typescript-eslint/eslint-plugin": "^5.5.0",
56
- "@typescript-eslint/parser": "^5.5.0",
57
- "ava": "^3.15.0",
58
- "eslint": "^8.3.0",
54
+ "@size-limit/preset-small-lib": "^7.0.5",
55
+ "@types/node-fetch": "^3.0.3",
56
+ "@types/sinon": "^10.0.9",
57
+ "@typescript-eslint/eslint-plugin": "^5.10.1",
58
+ "@typescript-eslint/parser": "^5.10.1",
59
+ "ava": "^4.0.1",
60
+ "eslint": "^8.7.0",
59
61
  "eslint-config-prettier": "^8.3.0",
60
62
  "eslint-plugin-prettier": "^4.0.0",
61
63
  "eslint-plugin-tsdoc": "^0.2.14",
62
- "msw": "^0.35.0",
64
+ "msw": "^0.36.7",
63
65
  "node-fetch": "^2.6.5",
64
66
  "nyc": "^15.1.0",
65
- "prettier": "^2.5.0",
67
+ "prettier": "^2.5.1",
66
68
  "prettier-plugin-jsdoc": "^0.3.30",
67
69
  "sinon": "^12.0.1",
68
70
  "siroc": "^0.16.0",
71
+ "size-limit": "^7.0.5",
69
72
  "standard-version": "^9.3.2",
70
73
  "ts-eager": "^2.0.2",
71
- "type-fest": "^2.8.0",
72
- "typescript": "^4.5.2"
74
+ "type-fest": "^2.11.0",
75
+ "typescript": "^4.5.5"
73
76
  },
74
77
  "engines": {
75
78
  "node": ">=12.13.0"
76
79
  },
77
80
  "publishConfig": {
78
81
  "access": "public"
79
- }
82
+ },
83
+ "react-native": "dist/index.js"
80
84
  }
@@ -0,0 +1,3 @@
1
+ import { PrismicError } from "./PrismicError";
2
+
3
+ export class NotFoundError extends PrismicError<undefined> {}
package/src/client.ts CHANGED
@@ -15,6 +15,7 @@ import { ParsingError } from "./ParsingError";
15
15
  import { PrismicError } from "./PrismicError";
16
16
  import { predicate } from "./predicate";
17
17
  import * as cookie from "./cookie";
18
+ import { NotFoundError } from "./NotFoundError";
18
19
 
19
20
  /**
20
21
  * The largest page size allowed by the Prismic REST API V2. This value is used
@@ -310,6 +311,17 @@ export class Client {
310
311
  * @returns A client that can query content from the repository.
311
312
  */
312
313
  constructor(endpoint: string, options: ClientConfig = {}) {
314
+ if (
315
+ process.env.NODE_ENV === "development" &&
316
+ /\.prismic\.io\/(?!api\/v2\/?)/.test(endpoint)
317
+ ) {
318
+ throw new PrismicError(
319
+ "@prismicio/client only supports Prismic Rest API V2. Please use the getEndpoint helper to generate a valid Rest API V2 endpoint URL.",
320
+ undefined,
321
+ undefined,
322
+ );
323
+ }
324
+
313
325
  this.endpoint = endpoint;
314
326
  this.accessToken = options.accessToken;
315
327
  this.routes = options.routes;
@@ -335,6 +347,8 @@ export class Client {
335
347
  if (this.fetchFn === globalThis.fetch) {
336
348
  this.fetchFn = this.fetchFn.bind(globalThis);
337
349
  }
350
+
351
+ this.graphqlFetch = this.graphqlFetch.bind(this);
338
352
  }
339
353
 
340
354
  /**
@@ -1153,6 +1167,7 @@ export class Client {
1153
1167
  if (documentID != null) {
1154
1168
  const document = await this.getByID(documentID, {
1155
1169
  ref: previewToken,
1170
+ lang: "*",
1156
1171
  });
1157
1172
 
1158
1173
  // We know we have a valid field to resolve since we are using prismicH.documentToLinkField
@@ -1249,6 +1264,70 @@ export class Client {
1249
1264
  };
1250
1265
  }
1251
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
+
1252
1331
  /**
1253
1332
  * Returns a cached version of `getRepository` with a TTL.
1254
1333
  *
@@ -1379,7 +1458,17 @@ export class Client {
1379
1458
  // Content Type. If not, this will throw, signaling an invalid response.
1380
1459
  json = await res.json();
1381
1460
  } catch {
1382
- throw new PrismicError(undefined, url, undefined);
1461
+ // Not Found (this response has an empty body and throws on `.json()`)
1462
+ // - Incorrect repository name
1463
+ if (res.status === 404) {
1464
+ throw new NotFoundError(
1465
+ `Prismic repository not found. Check that "${this.endpoint}" is pointing to the correct repository.`,
1466
+ url,
1467
+ undefined,
1468
+ );
1469
+ } else {
1470
+ throw new PrismicError(undefined, url, undefined);
1471
+ }
1383
1472
  }
1384
1473
 
1385
1474
  switch (res.status) {
@@ -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
 
@@ -21,6 +22,7 @@ export const Predicates = predicate;
21
22
  export { PrismicError } from "./PrismicError";
22
23
  export { ForbiddenError } from "./ForbiddenError";
23
24
  export { ParsingError } from "./ParsingError";
25
+ export { NotFoundError } from "./NotFoundError";
24
26
 
25
27
  // A collection of well-known cookie names shared between Prismic libraries and systems.
26
28
  export * as cookie from "./cookie";