@prismicio/client
Advanced tools
Comparing version 6.1.1 to 6.2.0
@@ -26,5 +26,20 @@ import * as prismicT from '@prismicio/types'; | ||
* A universal API to make network requests. A subset of the `fetch()` API. | ||
* | ||
* {@link https://developer.mozilla.org/en-US/docs/Web/API/fetch} | ||
*/ | ||
declare type FetchLike = (input: string, init?: RequestInitLike) => Promise<ResponseLike>; | ||
/** | ||
* An object that allows you to abort a `fetch()` request if needed via an | ||
* `AbortController` object | ||
* | ||
* {@link https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal} | ||
*/ | ||
declare type AbortSignalLike = { | ||
aborted: any; | ||
addEventListener: any; | ||
removeEventListener: any; | ||
dispatchEvent: any; | ||
onabort: any; | ||
}; | ||
/** | ||
* The minimum required properties from RequestInit. | ||
@@ -34,2 +49,3 @@ */ | ||
headers?: Record<string, string>; | ||
signal?: AbortSignalLike | null; | ||
} | ||
@@ -240,2 +256,15 @@ /** | ||
/** | ||
* Parameters for any client method that use `fetch()`. Only a subset of | ||
* `fetch()` parameters are exposed. | ||
*/ | ||
declare type FetchParams = { | ||
/** | ||
* An `AbortSignal` provided by an `AbortController`. This allows the network | ||
* request to be cancelled if necessary. | ||
* | ||
* {@link https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal} | ||
*/ | ||
signal?: AbortSignalLike; | ||
}; | ||
/** | ||
* Parameters specific to client methods that fetch all documents. These methods | ||
@@ -413,3 +442,3 @@ * start with `getAll` (for example, `getAllByType`). | ||
*/ | ||
query<TDocument extends prismicT.PrismicDocument>(predicates: NonNullable<BuildQueryURLArgs["predicates"]>, params?: Partial<Omit<BuildQueryURLArgs, "predicates">>): Promise<prismicT.Query<TDocument>>; | ||
query<TDocument extends prismicT.PrismicDocument>(predicates: NonNullable<BuildQueryURLArgs["predicates"]>, params?: Partial<Omit<BuildQueryURLArgs, "predicates">> & FetchParams): Promise<prismicT.Query<TDocument>>; | ||
/** | ||
@@ -429,3 +458,3 @@ * Queries content from the Prismic repository. | ||
*/ | ||
get<TDocument extends prismicT.PrismicDocument>(params?: Partial<BuildQueryURLArgs>): Promise<prismicT.Query<TDocument>>; | ||
get<TDocument extends prismicT.PrismicDocument>(params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<prismicT.Query<TDocument>>; | ||
/** | ||
@@ -445,3 +474,3 @@ * Queries content from the Prismic repository and returns only the first | ||
*/ | ||
getFirst<TDocument extends prismicT.PrismicDocument>(params?: Partial<BuildQueryURLArgs>): Promise<TDocument>; | ||
getFirst<TDocument extends prismicT.PrismicDocument>(params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<TDocument>; | ||
/** | ||
@@ -468,3 +497,3 @@ * **IMPORTANT**: Avoid using `dangerouslyGetAll` as it may be slower and | ||
*/ | ||
dangerouslyGetAll<TDocument extends prismicT.PrismicDocument>(params?: Partial<Omit<BuildQueryURLArgs, "page">> & GetAllParams): Promise<TDocument[]>; | ||
dangerouslyGetAll<TDocument extends prismicT.PrismicDocument>(params?: Partial<Omit<BuildQueryURLArgs, "page">> & GetAllParams & FetchParams): Promise<TDocument[]>; | ||
/** | ||
@@ -490,3 +519,3 @@ * Queries a document from the Prismic repository with a specific ID. | ||
*/ | ||
getByID<TDocument extends prismicT.PrismicDocument>(id: string, params?: Partial<BuildQueryURLArgs>): Promise<TDocument>; | ||
getByID<TDocument extends prismicT.PrismicDocument>(id: string, params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<TDocument>; | ||
/** | ||
@@ -515,3 +544,3 @@ * Queries documents from the Prismic repository with specific IDs. | ||
*/ | ||
getByIDs<TDocument extends prismicT.PrismicDocument>(ids: string[], params?: Partial<BuildQueryURLArgs>): Promise<prismicT.Query<TDocument>>; | ||
getByIDs<TDocument extends prismicT.PrismicDocument>(ids: string[], params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<prismicT.Query<TDocument>>; | ||
/** | ||
@@ -541,3 +570,3 @@ * Queries all documents from the Prismic repository with specific IDs. | ||
*/ | ||
getAllByIDs<TDocument extends prismicT.PrismicDocument>(ids: string[], params?: Partial<BuildQueryURLArgs>): Promise<TDocument[]>; | ||
getAllByIDs<TDocument extends prismicT.PrismicDocument>(ids: string[], params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<TDocument[]>; | ||
/** | ||
@@ -564,3 +593,3 @@ * Queries a document from the Prismic repository with a specific UID and Custom Type. | ||
*/ | ||
getByUID<TDocument extends prismicT.PrismicDocument>(documentType: string, uid: string, params?: Partial<BuildQueryURLArgs>): Promise<TDocument>; | ||
getByUID<TDocument extends prismicT.PrismicDocument>(documentType: string, uid: string, params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<TDocument>; | ||
/** | ||
@@ -590,3 +619,3 @@ * Queries document from the Prismic repository with specific UIDs and Custom Type. | ||
*/ | ||
getByUIDs<TDocument extends prismicT.PrismicDocument>(documentType: string, uids: string[], params?: Partial<BuildQueryURLArgs>): Promise<prismicT.Query<TDocument>>; | ||
getByUIDs<TDocument extends prismicT.PrismicDocument>(documentType: string, uids: string[], params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<prismicT.Query<TDocument>>; | ||
/** | ||
@@ -617,3 +646,3 @@ * Queries all documents from the Prismic repository with specific UIDs and Custom Type. | ||
*/ | ||
getAllByUIDs<TDocument extends prismicT.PrismicDocument>(documentType: string, uids: string[], params?: Partial<BuildQueryURLArgs>): Promise<TDocument[]>; | ||
getAllByUIDs<TDocument extends prismicT.PrismicDocument>(documentType: string, uids: string[], params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<TDocument[]>; | ||
/** | ||
@@ -639,3 +668,3 @@ * Queries a singleton document from the Prismic repository for a specific Custom Type. | ||
*/ | ||
getSingle<TDocument extends prismicT.PrismicDocument>(documentType: string, params?: Partial<BuildQueryURLArgs>): Promise<TDocument>; | ||
getSingle<TDocument extends prismicT.PrismicDocument>(documentType: string, params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<TDocument>; | ||
/** | ||
@@ -659,3 +688,3 @@ * Queries documents from the Prismic repository for a specific Custom Type. | ||
*/ | ||
getByType<TDocument extends prismicT.PrismicDocument>(documentType: string, params?: Partial<BuildQueryURLArgs>): Promise<prismicT.Query<TDocument>>; | ||
getByType<TDocument extends prismicT.PrismicDocument>(documentType: string, params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<prismicT.Query<TDocument>>; | ||
/** | ||
@@ -678,3 +707,3 @@ * Queries all documents from the Prismic repository for a specific Custom Type. | ||
*/ | ||
getAllByType<TDocument extends prismicT.PrismicDocument>(documentType: string, params?: Partial<Omit<BuildQueryURLArgs, "page">>): Promise<TDocument[]>; | ||
getAllByType<TDocument extends prismicT.PrismicDocument>(documentType: string, params?: Partial<Omit<BuildQueryURLArgs, "page">> & FetchParams): Promise<TDocument[]>; | ||
/** | ||
@@ -697,3 +726,3 @@ * Queries documents from the Prismic repository with a specific tag. | ||
*/ | ||
getByTag<TDocument extends prismicT.PrismicDocument>(tag: string, params?: Partial<BuildQueryURLArgs>): Promise<prismicT.Query<TDocument>>; | ||
getByTag<TDocument extends prismicT.PrismicDocument>(tag: string, params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<prismicT.Query<TDocument>>; | ||
/** | ||
@@ -716,3 +745,3 @@ * Queries all documents from the Prismic repository with a specific tag. | ||
*/ | ||
getAllByTag<TDocument extends prismicT.PrismicDocument>(tag: string, params?: Partial<Omit<BuildQueryURLArgs, "page">>): Promise<TDocument[]>; | ||
getAllByTag<TDocument extends prismicT.PrismicDocument>(tag: string, params?: Partial<Omit<BuildQueryURLArgs, "page">> & FetchParams): Promise<TDocument[]>; | ||
/** | ||
@@ -734,3 +763,3 @@ * Queries documents from the Prismic repository with specific tags. A | ||
*/ | ||
getByEveryTag<TDocument extends prismicT.PrismicDocument>(tags: string[], params?: Partial<BuildQueryURLArgs>): Promise<prismicT.Query<TDocument>>; | ||
getByEveryTag<TDocument extends prismicT.PrismicDocument>(tags: string[], params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<prismicT.Query<TDocument>>; | ||
/** | ||
@@ -754,3 +783,3 @@ * Queries documents from the Prismic repository with specific tags. A | ||
*/ | ||
getAllByEveryTag<TDocument extends prismicT.PrismicDocument>(tags: string[], params?: Partial<Omit<BuildQueryURLArgs, "page">>): Promise<TDocument[]>; | ||
getAllByEveryTag<TDocument extends prismicT.PrismicDocument>(tags: string[], params?: Partial<Omit<BuildQueryURLArgs, "page">> & FetchParams): Promise<TDocument[]>; | ||
/** | ||
@@ -772,3 +801,3 @@ * Queries documents from the Prismic repository with specific tags. A | ||
*/ | ||
getBySomeTags<TDocument extends prismicT.PrismicDocument>(tags: string[], params?: Partial<BuildQueryURLArgs>): Promise<prismicT.Query<TDocument>>; | ||
getBySomeTags<TDocument extends prismicT.PrismicDocument>(tags: string[], params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<prismicT.Query<TDocument>>; | ||
/** | ||
@@ -792,3 +821,3 @@ * Queries documents from the Prismic repository with specific tags. A | ||
*/ | ||
getAllBySomeTags<TDocument extends prismicT.PrismicDocument>(tags: string[], params?: Partial<Omit<BuildQueryURLArgs, "page">>): Promise<TDocument[]>; | ||
getAllBySomeTags<TDocument extends prismicT.PrismicDocument>(tags: string[], params?: Partial<Omit<BuildQueryURLArgs, "page">> & FetchParams): Promise<TDocument[]>; | ||
/** | ||
@@ -800,3 +829,3 @@ * Returns metadata about the Prismic repository, such as its refs, releases, | ||
*/ | ||
getRepository(): Promise<prismicT.Repository>; | ||
getRepository(params?: FetchParams): Promise<prismicT.Repository>; | ||
/** | ||
@@ -811,3 +840,3 @@ * Returns a list of all refs for the Prismic repository. | ||
*/ | ||
getRefs(): Promise<prismicT.Ref[]>; | ||
getRefs(params?: FetchParams): Promise<prismicT.Ref[]>; | ||
/** | ||
@@ -820,3 +849,3 @@ * Returns a ref for the Prismic repository with a matching ID. | ||
*/ | ||
getRefByID(id: string): Promise<prismicT.Ref>; | ||
getRefByID(id: string, params?: FetchParams): Promise<prismicT.Ref>; | ||
/** | ||
@@ -829,3 +858,3 @@ * Returns a ref for the Prismic repository with a matching label. | ||
*/ | ||
getRefByLabel(label: string): Promise<prismicT.Ref>; | ||
getRefByLabel(label: string, params?: FetchParams): Promise<prismicT.Ref>; | ||
/** | ||
@@ -837,3 +866,3 @@ * Returns the master ref for the Prismic repository. The master ref points to | ||
*/ | ||
getMasterRef(): Promise<prismicT.Ref>; | ||
getMasterRef(params?: FetchParams): Promise<prismicT.Ref>; | ||
/** | ||
@@ -845,3 +874,3 @@ * Returns a list of all Releases for the Prismic repository. Releases are | ||
*/ | ||
getReleases(): Promise<prismicT.Ref[]>; | ||
getReleases(params?: FetchParams): Promise<prismicT.Ref[]>; | ||
/** | ||
@@ -854,3 +883,3 @@ * Returns a Release for the Prismic repository with a matching ID. | ||
*/ | ||
getReleaseByID(id: string): Promise<prismicT.Ref>; | ||
getReleaseByID(id: string, params?: FetchParams): Promise<prismicT.Ref>; | ||
/** | ||
@@ -863,3 +892,3 @@ * Returns a Release for the Prismic repository with a matching label. | ||
*/ | ||
getReleaseByLabel(label: string): Promise<prismicT.Ref>; | ||
getReleaseByLabel(label: string, params?: FetchParams): Promise<prismicT.Ref>; | ||
/** | ||
@@ -870,3 +899,3 @@ * Returns a list of all tags used in the Prismic repository. | ||
*/ | ||
getTags(): Promise<string[]>; | ||
getTags(params?: FetchParams): Promise<string[]>; | ||
/** | ||
@@ -879,3 +908,3 @@ * Builds a URL used to query content from the Prismic repository. | ||
*/ | ||
buildQueryURL(params?: Partial<BuildQueryURLArgs>): Promise<string>; | ||
buildQueryURL({ signal, ...params }?: Partial<BuildQueryURLArgs> & FetchParams): Promise<string>; | ||
/** | ||
@@ -900,3 +929,3 @@ * Determines the URL for a previewed document during an active preview | ||
*/ | ||
resolvePreviewURL(args: ResolvePreviewArgs): Promise<string>; | ||
resolvePreviewURL(args: ResolvePreviewArgs & FetchParams): Promise<string>; | ||
/** | ||
@@ -967,3 +996,4 @@ * Configures the client to query the latest published content for all future queries. | ||
* Prismic's GraphQL API. It automatically applies the necessary `prismic-ref` | ||
* and Authorization headers. | ||
* and Authorization headers. Queries will automatically be minified by | ||
* removing whitespace where possible. | ||
* | ||
@@ -970,0 +1000,0 @@ * @example |
@@ -103,5 +103,19 @@ import * as prismicH from '@prismicio/helpers'; | ||
const minifyGraphQLQuery = (query) => { | ||
return query.replace(/(\n| )*( |{|})(\n| )*/gm, (_chars, _spaces, brackets) => brackets); | ||
}; | ||
const preview = "io.prismic.preview"; | ||
var cookie = /*#__PURE__*/Object.freeze({ | ||
__proto__: null, | ||
preview: preview | ||
}); | ||
class ForbiddenError extends PrismicError { | ||
} | ||
class NotFoundError extends PrismicError { | ||
} | ||
class ParsingError extends PrismicError { | ||
@@ -175,12 +189,2 @@ } | ||
const preview = "io.prismic.preview"; | ||
var cookie = /*#__PURE__*/Object.freeze({ | ||
__proto__: null, | ||
preview: preview | ||
}); | ||
class NotFoundError extends PrismicError { | ||
} | ||
const MAX_PAGE_SIZE = 100; | ||
@@ -190,4 +194,4 @@ const REPOSITORY_CACHE_TTL = 5e3; | ||
const typePredicate = (documentType) => predicate.at("document.type", documentType); | ||
const everyTagPredicate = (tags) => predicate.at("document.tags", tags); | ||
const someTagsPredicate = (tags) => predicate.any("document.tags", tags); | ||
const everyTagPredicate = (tags) => predicate.at("document.tags", castArray(tags)); | ||
const someTagsPredicate = (tags) => predicate.any("document.tags", castArray(tags)); | ||
const createClient = (...args) => new Client(...args); | ||
@@ -305,6 +309,6 @@ class Client { | ||
async getByTag(tag, params) { | ||
return await this.get(appendPredicates(params, everyTagPredicate(tag))); | ||
return await this.get(appendPredicates(params, someTagsPredicate(tag))); | ||
} | ||
async getAllByTag(tag, params) { | ||
return await this.dangerouslyGetAll(appendPredicates(params, everyTagPredicate(tag))); | ||
return await this.dangerouslyGetAll(appendPredicates(params, someTagsPredicate(tag))); | ||
} | ||
@@ -323,3 +327,3 @@ async getByEveryTag(tags, params) { | ||
} | ||
async getRepository() { | ||
async getRepository(params) { | ||
const url = new URL(this.endpoint); | ||
@@ -329,44 +333,47 @@ if (this.accessToken) { | ||
} | ||
return await this.fetch(url.toString()); | ||
return await this.fetch(url.toString(), params); | ||
} | ||
async getRefs() { | ||
const repository = await this.getRepository(); | ||
async getRefs(params) { | ||
const repository = await this.getRepository(params); | ||
return repository.refs; | ||
} | ||
async getRefByID(id) { | ||
const refs = await this.getRefs(); | ||
async getRefByID(id, params) { | ||
const refs = await this.getRefs(params); | ||
return findRefByID(refs, id); | ||
} | ||
async getRefByLabel(label) { | ||
const refs = await this.getRefs(); | ||
async getRefByLabel(label, params) { | ||
const refs = await this.getRefs(params); | ||
return findRefByLabel(refs, label); | ||
} | ||
async getMasterRef() { | ||
const refs = await this.getRefs(); | ||
async getMasterRef(params) { | ||
const refs = await this.getRefs(params); | ||
return findMasterRef(refs); | ||
} | ||
async getReleases() { | ||
const refs = await this.getRefs(); | ||
async getReleases(params) { | ||
const refs = await this.getRefs(params); | ||
return refs.filter((ref) => !ref.isMasterRef); | ||
} | ||
async getReleaseByID(id) { | ||
const releases = await this.getReleases(); | ||
async getReleaseByID(id, params) { | ||
const releases = await this.getReleases(params); | ||
return findRefByID(releases, id); | ||
} | ||
async getReleaseByLabel(label) { | ||
const releases = await this.getReleases(); | ||
async getReleaseByLabel(label, params) { | ||
const releases = await this.getReleases(params); | ||
return findRefByLabel(releases, label); | ||
} | ||
async getTags() { | ||
async getTags(params) { | ||
try { | ||
const tagsForm = await this.getCachedRepositoryForm("tags"); | ||
const tagsForm = await this.getCachedRepositoryForm("tags", params); | ||
return await this.fetch(tagsForm.action); | ||
} catch (e) { | ||
const repository = await this.getRepository(); | ||
const repository = await this.getRepository(params); | ||
return repository.tags; | ||
} | ||
} | ||
async buildQueryURL(params = {}) { | ||
async buildQueryURL({ | ||
signal, | ||
...params | ||
} = {}) { | ||
const ref = params.ref || await this.getResolvedRefString(); | ||
const integrationFieldsRef = params.integrationFieldsRef || (await this.getCachedRepository()).integrationFieldsRef || void 0; | ||
const integrationFieldsRef = params.integrationFieldsRef || (await this.getCachedRepository({ signal })).integrationFieldsRef || void 0; | ||
return buildQueryURL(this.endpoint, { | ||
@@ -395,2 +402,3 @@ ...this.defaultParams, | ||
const document = await this.getByID(documentID, { | ||
signal: args.signal, | ||
ref: previewToken, | ||
@@ -443,3 +451,8 @@ lang: "*" | ||
} | ||
return await this.fetchFn(input, { | ||
const url = new URL(input); | ||
const query = url.searchParams.get("query"); | ||
if (query) { | ||
url.searchParams.set("query", minifyGraphQLQuery(query)); | ||
} | ||
return await this.fetchFn(url.toString(), { | ||
...init, | ||
@@ -449,11 +462,11 @@ headers | ||
} | ||
async getCachedRepository() { | ||
async getCachedRepository(params) { | ||
if (!this.cachedRepository || Date.now() >= this.cachedRepositoryExpiration) { | ||
this.cachedRepositoryExpiration = Date.now() + REPOSITORY_CACHE_TTL; | ||
this.cachedRepository = await this.getRepository(); | ||
this.cachedRepository = await this.getRepository(params); | ||
} | ||
return this.cachedRepository; | ||
} | ||
async getCachedRepositoryForm(name) { | ||
const cachedRepository = await this.getCachedRepository(); | ||
async getCachedRepositoryForm(name, params) { | ||
const cachedRepository = await this.getCachedRepository(params); | ||
const form = cachedRepository.forms[name]; | ||
@@ -465,3 +478,3 @@ if (!form) { | ||
} | ||
async getResolvedRefString() { | ||
async getResolvedRefString(params) { | ||
var _a, _b, _c; | ||
@@ -479,3 +492,3 @@ if (this.refState.autoPreviewsEnabled) { | ||
} | ||
const cachedRepository = await this.getCachedRepository(); | ||
const cachedRepository = await this.getCachedRepository(params); | ||
const refModeType = this.refState.mode; | ||
@@ -494,4 +507,6 @@ if (refModeType === "ReleaseID" /* ReleaseID */) { | ||
} | ||
async fetch(url, _params) { | ||
const res = await this.fetchFn(url); | ||
async fetch(url, params = {}) { | ||
const res = await this.fetchFn(url, { | ||
signal: params.signal | ||
}); | ||
let json; | ||
@@ -498,0 +513,0 @@ try { |
{ | ||
"name": "@prismicio/client", | ||
"version": "6.1.1", | ||
"version": "6.2.0", | ||
"description": "The official JavaScript + TypeScript client library for Prismic", | ||
@@ -50,27 +50,27 @@ "keywords": [ | ||
"dependencies": { | ||
"@prismicio/helpers": "^2.0.1", | ||
"@prismicio/types": "^0.1.22" | ||
"@prismicio/helpers": "^2.1.1", | ||
"@prismicio/types": "^0.1.23" | ||
}, | ||
"devDependencies": { | ||
"@size-limit/preset-small-lib": "^7.0.5", | ||
"@types/node-fetch": "^3.0.3", | ||
"@types/sinon": "^10.0.9", | ||
"@typescript-eslint/eslint-plugin": "^5.10.1", | ||
"@typescript-eslint/parser": "^5.10.1", | ||
"@size-limit/preset-small-lib": "^7.0.8", | ||
"@types/node-fetch": "^2.5.12", | ||
"@types/sinon": "^10.0.10", | ||
"@typescript-eslint/eslint-plugin": "^5.10.2", | ||
"@typescript-eslint/parser": "^5.10.2", | ||
"abort-controller": "^3.0.0", | ||
"ava": "^4.0.1", | ||
"eslint": "^8.7.0", | ||
"eslint": "^8.8.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-prettier": "^4.0.0", | ||
"eslint-plugin-tsdoc": "^0.2.14", | ||
"msw": "^0.36.7", | ||
"node-fetch": "^2.6.5", | ||
"msw": "^0.36.8", | ||
"node-fetch": "^2.6.7", | ||
"nyc": "^15.1.0", | ||
"prettier": "^2.5.1", | ||
"prettier-plugin-jsdoc": "^0.3.30", | ||
"sinon": "^12.0.1", | ||
"sinon": "^13.0.1", | ||
"siroc": "^0.16.0", | ||
"size-limit": "^7.0.5", | ||
"size-limit": "^7.0.8", | ||
"standard-version": "^9.3.2", | ||
"ts-eager": "^2.0.2", | ||
"type-fest": "^2.11.0", | ||
"typescript": "^4.5.5" | ||
@@ -77,0 +77,0 @@ }, |
@@ -75,3 +75,3 @@ # @prismicio/client | ||
[prismic-previews]: https://prismic.io/docs/core-concepts/previews | ||
[prismic-docs]: https://prismic.io/docs/technical-reference/prismicio-client?version=v6 | ||
[prismic-docs]: https://prismic.io/docs/technical-reference/prismicio-client | ||
[changelog]: ./CHANGELOG.md | ||
@@ -78,0 +78,0 @@ [contributing]: ./CONTRIBUTING.md |
@@ -1,6 +0,4 @@ | ||
import { ValueOf } from "type-fest"; | ||
import { castArray } from "./lib/castArray"; | ||
import { Ordering, Route } from "./types"; | ||
import { ValueOf, Ordering, Route } from "./types"; | ||
@@ -7,0 +5,0 @@ /** |
@@ -5,2 +5,3 @@ import * as prismicT from "@prismicio/types"; | ||
import { appendPredicates } from "./lib/appendPredicates"; | ||
import { castArray } from "./lib/castArray"; | ||
import { castThunk } from "./lib/castThunk"; | ||
@@ -11,11 +12,12 @@ import { findMasterRef } from "./lib/findMasterRef"; | ||
import { getCookie } from "./lib/getCookie"; | ||
import { minifyGraphQLQuery } from "./lib/minifyGraphQLQuery"; | ||
import { FetchLike, HttpRequestLike } from "./types"; | ||
import { buildQueryURL, BuildQueryURLArgs } from "./buildQueryURL"; | ||
import * as cookie from "./cookie"; | ||
import { AbortSignalLike, FetchLike, HttpRequestLike } from "./types"; | ||
import { ForbiddenError } from "./ForbiddenError"; | ||
import { NotFoundError } from "./NotFoundError"; | ||
import { ParsingError } from "./ParsingError"; | ||
import { PrismicError } from "./PrismicError"; | ||
import { buildQueryURL, BuildQueryURLArgs } from "./buildQueryURL"; | ||
import { predicate } from "./predicate"; | ||
import * as cookie from "./cookie"; | ||
import { NotFoundError } from "./NotFoundError"; | ||
@@ -154,2 +156,16 @@ /** | ||
/** | ||
* Parameters for any client method that use `fetch()`. Only a subset of | ||
* `fetch()` parameters are exposed. | ||
*/ | ||
type FetchParams = { | ||
/** | ||
* An `AbortSignal` provided by an `AbortController`. This allows the network | ||
* request to be cancelled if necessary. | ||
* | ||
* {@link https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal} | ||
*/ | ||
signal?: AbortSignalLike; | ||
}; | ||
/** | ||
* Parameters specific to client methods that fetch all documents. These methods | ||
@@ -211,3 +227,3 @@ * start with `getAll` (for example, `getAllByType`). | ||
const everyTagPredicate = (tags: string | string[]): string => | ||
predicate.at("document.tags", tags); | ||
predicate.at("document.tags", castArray(tags)); | ||
@@ -223,3 +239,3 @@ /** | ||
const someTagsPredicate = (tags: string | string[]): string => | ||
predicate.any("document.tags", tags); | ||
predicate.any("document.tags", castArray(tags)); | ||
@@ -435,3 +451,3 @@ /** | ||
predicates: NonNullable<BuildQueryURLArgs["predicates"]>, | ||
params?: Partial<Omit<BuildQueryURLArgs, "predicates">>, | ||
params?: Partial<Omit<BuildQueryURLArgs, "predicates">> & FetchParams, | ||
): Promise<prismicT.Query<TDocument>> { | ||
@@ -458,3 +474,3 @@ const url = await this.buildQueryURL({ ...params, predicates }); | ||
async get<TDocument extends prismicT.PrismicDocument>( | ||
params?: Partial<BuildQueryURLArgs>, | ||
params?: Partial<BuildQueryURLArgs> & FetchParams, | ||
): Promise<prismicT.Query<TDocument>> { | ||
@@ -481,3 +497,3 @@ const url = await this.buildQueryURL(params); | ||
async getFirst<TDocument extends prismicT.PrismicDocument>( | ||
params?: Partial<BuildQueryURLArgs>, | ||
params?: Partial<BuildQueryURLArgs> & FetchParams, | ||
): Promise<TDocument> { | ||
@@ -518,3 +534,5 @@ const url = await this.buildQueryURL(params); | ||
async dangerouslyGetAll<TDocument extends prismicT.PrismicDocument>( | ||
params: Partial<Omit<BuildQueryURLArgs, "page">> & GetAllParams = {}, | ||
params: Partial<Omit<BuildQueryURLArgs, "page">> & | ||
GetAllParams & | ||
FetchParams = {}, | ||
): Promise<TDocument[]> { | ||
@@ -569,3 +587,3 @@ const { limit = Infinity, ...actualParams } = params; | ||
id: string, | ||
params?: Partial<BuildQueryURLArgs>, | ||
params?: Partial<BuildQueryURLArgs> & FetchParams, | ||
): Promise<TDocument> { | ||
@@ -602,3 +620,3 @@ return await this.getFirst<TDocument>( | ||
ids: string[], | ||
params?: Partial<BuildQueryURLArgs>, | ||
params?: Partial<BuildQueryURLArgs> & FetchParams, | ||
): Promise<prismicT.Query<TDocument>> { | ||
@@ -636,3 +654,3 @@ return await this.get<TDocument>( | ||
ids: string[], | ||
params?: Partial<BuildQueryURLArgs>, | ||
params?: Partial<BuildQueryURLArgs> & FetchParams, | ||
): Promise<TDocument[]> { | ||
@@ -668,3 +686,3 @@ return await this.dangerouslyGetAll<TDocument>( | ||
uid: string, | ||
params?: Partial<BuildQueryURLArgs>, | ||
params?: Partial<BuildQueryURLArgs> & FetchParams, | ||
): Promise<TDocument> { | ||
@@ -706,3 +724,3 @@ return await this.getFirst<TDocument>( | ||
uids: string[], | ||
params?: Partial<BuildQueryURLArgs>, | ||
params?: Partial<BuildQueryURLArgs> & FetchParams, | ||
): Promise<prismicT.Query<TDocument>> { | ||
@@ -745,3 +763,3 @@ return await this.get<TDocument>( | ||
uids: string[], | ||
params?: Partial<BuildQueryURLArgs>, | ||
params?: Partial<BuildQueryURLArgs> & FetchParams, | ||
): Promise<TDocument[]> { | ||
@@ -778,3 +796,3 @@ return await this.dangerouslyGetAll<TDocument>( | ||
documentType: string, | ||
params?: Partial<BuildQueryURLArgs>, | ||
params?: Partial<BuildQueryURLArgs> & FetchParams, | ||
): Promise<TDocument> { | ||
@@ -806,3 +824,3 @@ return await this.getFirst<TDocument>( | ||
documentType: string, | ||
params?: Partial<BuildQueryURLArgs>, | ||
params?: Partial<BuildQueryURLArgs> & FetchParams, | ||
): Promise<prismicT.Query<TDocument>> { | ||
@@ -833,3 +851,3 @@ return await this.get<TDocument>( | ||
documentType: string, | ||
params?: Partial<Omit<BuildQueryURLArgs, "page">>, | ||
params?: Partial<Omit<BuildQueryURLArgs, "page">> & FetchParams, | ||
): Promise<TDocument[]> { | ||
@@ -860,6 +878,6 @@ return await this.dangerouslyGetAll<TDocument>( | ||
tag: string, | ||
params?: Partial<BuildQueryURLArgs>, | ||
params?: Partial<BuildQueryURLArgs> & FetchParams, | ||
): Promise<prismicT.Query<TDocument>> { | ||
return await this.get<TDocument>( | ||
appendPredicates(params, everyTagPredicate(tag)), | ||
appendPredicates(params, someTagsPredicate(tag)), | ||
); | ||
@@ -887,6 +905,6 @@ } | ||
tag: string, | ||
params?: Partial<Omit<BuildQueryURLArgs, "page">>, | ||
params?: Partial<Omit<BuildQueryURLArgs, "page">> & FetchParams, | ||
): Promise<TDocument[]> { | ||
return await this.dangerouslyGetAll<TDocument>( | ||
appendPredicates(params, everyTagPredicate(tag)), | ||
appendPredicates(params, someTagsPredicate(tag)), | ||
); | ||
@@ -913,3 +931,3 @@ } | ||
tags: string[], | ||
params?: Partial<BuildQueryURLArgs>, | ||
params?: Partial<BuildQueryURLArgs> & FetchParams, | ||
): Promise<prismicT.Query<TDocument>> { | ||
@@ -941,3 +959,3 @@ return await this.get<TDocument>( | ||
tags: string[], | ||
params?: Partial<Omit<BuildQueryURLArgs, "page">>, | ||
params?: Partial<Omit<BuildQueryURLArgs, "page">> & FetchParams, | ||
): Promise<TDocument[]> { | ||
@@ -967,3 +985,3 @@ return await this.dangerouslyGetAll<TDocument>( | ||
tags: string[], | ||
params?: Partial<BuildQueryURLArgs>, | ||
params?: Partial<BuildQueryURLArgs> & FetchParams, | ||
): Promise<prismicT.Query<TDocument>> { | ||
@@ -995,3 +1013,3 @@ return await this.get<TDocument>( | ||
tags: string[], | ||
params?: Partial<Omit<BuildQueryURLArgs, "page">>, | ||
params?: Partial<Omit<BuildQueryURLArgs, "page">> & FetchParams, | ||
): Promise<TDocument[]> { | ||
@@ -1009,3 +1027,3 @@ return await this.dangerouslyGetAll<TDocument>( | ||
*/ | ||
async getRepository(): Promise<prismicT.Repository> { | ||
async getRepository(params?: FetchParams): Promise<prismicT.Repository> { | ||
// TODO: Restore when Authorization header support works in browsers with CORS. | ||
@@ -1020,3 +1038,3 @@ // return await this.fetch<prismicT.Repository>(this.endpoint); | ||
return await this.fetch<prismicT.Repository>(url.toString()); | ||
return await this.fetch<prismicT.Repository>(url.toString(), params); | ||
} | ||
@@ -1033,4 +1051,4 @@ | ||
*/ | ||
async getRefs(): Promise<prismicT.Ref[]> { | ||
const repository = await this.getRepository(); | ||
async getRefs(params?: FetchParams): Promise<prismicT.Ref[]> { | ||
const repository = await this.getRepository(params); | ||
@@ -1047,4 +1065,4 @@ return repository.refs; | ||
*/ | ||
async getRefByID(id: string): Promise<prismicT.Ref> { | ||
const refs = await this.getRefs(); | ||
async getRefByID(id: string, params?: FetchParams): Promise<prismicT.Ref> { | ||
const refs = await this.getRefs(params); | ||
@@ -1061,4 +1079,7 @@ return findRefByID(refs, id); | ||
*/ | ||
async getRefByLabel(label: string): Promise<prismicT.Ref> { | ||
const refs = await this.getRefs(); | ||
async getRefByLabel( | ||
label: string, | ||
params?: FetchParams, | ||
): Promise<prismicT.Ref> { | ||
const refs = await this.getRefs(params); | ||
@@ -1074,4 +1095,4 @@ return findRefByLabel(refs, label); | ||
*/ | ||
async getMasterRef(): Promise<prismicT.Ref> { | ||
const refs = await this.getRefs(); | ||
async getMasterRef(params?: FetchParams): Promise<prismicT.Ref> { | ||
const refs = await this.getRefs(params); | ||
@@ -1087,4 +1108,4 @@ return findMasterRef(refs); | ||
*/ | ||
async getReleases(): Promise<prismicT.Ref[]> { | ||
const refs = await this.getRefs(); | ||
async getReleases(params?: FetchParams): Promise<prismicT.Ref[]> { | ||
const refs = await this.getRefs(params); | ||
@@ -1101,4 +1122,7 @@ return refs.filter((ref) => !ref.isMasterRef); | ||
*/ | ||
async getReleaseByID(id: string): Promise<prismicT.Ref> { | ||
const releases = await this.getReleases(); | ||
async getReleaseByID( | ||
id: string, | ||
params?: FetchParams, | ||
): Promise<prismicT.Ref> { | ||
const releases = await this.getReleases(params); | ||
@@ -1115,4 +1139,7 @@ return findRefByID(releases, id); | ||
*/ | ||
async getReleaseByLabel(label: string): Promise<prismicT.Ref> { | ||
const releases = await this.getReleases(); | ||
async getReleaseByLabel( | ||
label: string, | ||
params?: FetchParams, | ||
): Promise<prismicT.Ref> { | ||
const releases = await this.getReleases(params); | ||
@@ -1127,9 +1154,9 @@ return findRefByLabel(releases, label); | ||
*/ | ||
async getTags(): Promise<string[]> { | ||
async getTags(params?: FetchParams): Promise<string[]> { | ||
try { | ||
const tagsForm = await this.getCachedRepositoryForm("tags"); | ||
const tagsForm = await this.getCachedRepositoryForm("tags", params); | ||
return await this.fetch<string[]>(tagsForm.action); | ||
} catch { | ||
const repository = await this.getRepository(); | ||
const repository = await this.getRepository(params); | ||
@@ -1147,9 +1174,10 @@ return repository.tags; | ||
*/ | ||
async buildQueryURL( | ||
params: Partial<BuildQueryURLArgs> = {}, | ||
): Promise<string> { | ||
async buildQueryURL({ | ||
signal, | ||
...params | ||
}: Partial<BuildQueryURLArgs> & FetchParams = {}): Promise<string> { | ||
const ref = params.ref || (await this.getResolvedRefString()); | ||
const integrationFieldsRef = | ||
params.integrationFieldsRef || | ||
(await this.getCachedRepository()).integrationFieldsRef || | ||
(await this.getCachedRepository({ signal })).integrationFieldsRef || | ||
undefined; | ||
@@ -1186,3 +1214,5 @@ | ||
*/ | ||
async resolvePreviewURL(args: ResolvePreviewArgs): Promise<string> { | ||
async resolvePreviewURL( | ||
args: ResolvePreviewArgs & FetchParams, | ||
): Promise<string> { | ||
let documentID = args.documentID; | ||
@@ -1205,2 +1235,3 @@ let previewToken = args.previewToken; | ||
const document = await this.getByID(documentID, { | ||
signal: args.signal, | ||
ref: previewToken, | ||
@@ -1307,3 +1338,4 @@ lang: "*", | ||
* Prismic's GraphQL API. It automatically applies the necessary `prismic-ref` | ||
* and Authorization headers. | ||
* and Authorization headers. Queries will automatically be minified by | ||
* removing whitespace where possible. | ||
* | ||
@@ -1349,2 +1381,5 @@ * @example | ||
// Normalize header keys to lowercase. This prevents header | ||
// conflicts between the Prismic client and the GraphQL | ||
// client. | ||
const headers: Record<string, string> = {}; | ||
@@ -1358,3 +1393,6 @@ for (const key in unsanitizedHeaders) { | ||
return (await this.fetchFn( | ||
// Compress the GraphQL query (if it exists) by removing | ||
// whitespace. This is done to optimize the query size and avoid hitting the | ||
// upper limit of GET requests (2048 characters). | ||
const url = new URL( | ||
// Asserting `input` is a string since popular GraphQL | ||
@@ -1364,7 +1402,12 @@ // libraries pass this as a string. Request objects as | ||
input as string, | ||
{ | ||
...init, | ||
headers, | ||
}, | ||
)) as Response; | ||
); | ||
const query = url.searchParams.get("query"); | ||
if (query) { | ||
url.searchParams.set("query", minifyGraphQLQuery(query)); | ||
} | ||
return (await this.fetchFn(url.toString(), { | ||
...init, | ||
headers, | ||
})) as Response; | ||
} | ||
@@ -1377,3 +1420,5 @@ | ||
*/ | ||
private async getCachedRepository(): Promise<prismicT.Repository> { | ||
private async getCachedRepository( | ||
params?: FetchParams, | ||
): Promise<prismicT.Repository> { | ||
if ( | ||
@@ -1384,3 +1429,3 @@ !this.cachedRepository || | ||
this.cachedRepositoryExpiration = Date.now() + REPOSITORY_CACHE_TTL; | ||
this.cachedRepository = await this.getRepository(); | ||
this.cachedRepository = await this.getRepository(params); | ||
} | ||
@@ -1400,4 +1445,7 @@ | ||
*/ | ||
private async getCachedRepositoryForm(name: string): Promise<prismicT.Form> { | ||
const cachedRepository = await this.getCachedRepository(); | ||
private async getCachedRepositoryForm( | ||
name: string, | ||
params?: FetchParams, | ||
): Promise<prismicT.Form> { | ||
const cachedRepository = await this.getCachedRepository(params); | ||
const form = cachedRepository.forms[name]; | ||
@@ -1437,3 +1485,3 @@ | ||
*/ | ||
private async getResolvedRefString(): Promise<string> { | ||
private async getResolvedRefString(params?: FetchParams): Promise<string> { | ||
if (this.refState.autoPreviewsEnabled) { | ||
@@ -1456,3 +1504,3 @@ let previewRef: string | undefined = undefined; | ||
const cachedRepository = await this.getCachedRepository(); | ||
const cachedRepository = await this.getCachedRepository(params); | ||
@@ -1490,3 +1538,4 @@ const refModeType = this.refState.mode; | ||
// TODO: Change to `params` when Authorization header support works in browsers with CORS. | ||
_params?: Partial<BuildQueryURLArgs>, | ||
// _params?: Partial<BuildQueryURLArgs>, | ||
params: FetchParams = {}, | ||
): Promise<T> { | ||
@@ -1499,3 +1548,5 @@ // TODO: Restore when Authorization header support works in browsers with CORS. | ||
const res = await this.fetchFn(url); | ||
const res = await this.fetchFn(url, { | ||
signal: params.signal, | ||
}); | ||
@@ -1502,0 +1553,0 @@ // eslint-disable-next-line @typescript-eslint/no-explicit-any |
/** | ||
* Create a union of the given object's values, and optionally specify which | ||
* keys to get the values from. | ||
* | ||
* Taken from the `type-fest` package. | ||
* | ||
* See: | ||
* https://github.com/sindresorhus/type-fest/blob/61c35052f09caa23de5eef96d95196375d8ed498/source/value-of.d.ts | ||
*/ | ||
export type ValueOf< | ||
ObjectType, | ||
ValueType extends keyof ObjectType = keyof ObjectType, | ||
> = ObjectType[ValueType]; | ||
/** | ||
* A universal API to make network requests. A subset of the `fetch()` API. | ||
* | ||
* {@link https://developer.mozilla.org/en-US/docs/Web/API/fetch} | ||
*/ | ||
@@ -10,2 +26,25 @@ export type FetchLike = ( | ||
/** | ||
* An object that allows you to abort a `fetch()` request if needed via an | ||
* `AbortController` object | ||
* | ||
* {@link https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal} | ||
*/ | ||
// `any` is used often here to ensure this type is universally valid amond | ||
// different AbortSignal implementations. The types of each property are not | ||
// important to validate since it is blindly passed to a given `fetch()` | ||
// function. | ||
export type AbortSignalLike = { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
aborted: any; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
addEventListener: any; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
removeEventListener: any; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
dispatchEvent: any; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
onabort: any; | ||
}; | ||
/** | ||
* The minimum required properties from RequestInit. | ||
@@ -15,2 +54,5 @@ */ | ||
headers?: Record<string, string>; | ||
// `null` is included to match TypeScript's `fetch()` type in `lib.dom.d.ts`. | ||
// See: https://github.com/microsoft/TypeScript/blob/3328feb7991f358e245088d48b64ad9da8f015e2/lib/lib.dom.d.ts#L1515-L1518 | ||
signal?: AbortSignalLike | null; | ||
} | ||
@@ -17,0 +59,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
371413
30
4718
Updated@prismicio/helpers@^2.1.1
Updated@prismicio/types@^0.1.23