@prismicio/client
Advanced tools
Comparing version 7.0.0-alpha.2 to 7.0.0-alpha.3
@@ -139,3 +139,3 @@ import type { Query } from "./types/api/query"; | ||
fetch?: FetchLike; | ||
}; | ||
} & OptimizeParams; | ||
/** | ||
@@ -155,2 +155,30 @@ * Parameters for any client method that use `fetch()`. Only a subset of | ||
/** | ||
* Parameters that determine if or how the client is optimized. | ||
*/ | ||
type OptimizeParams = { | ||
/** | ||
* Options that determine if or how the client is optimized. | ||
*/ | ||
optimize?: { | ||
/** | ||
* Determines if fetching repository metadata should be optimized. The | ||
* optimizations result in faster queries, less network requests, and more | ||
* accurate cache-busting. | ||
* | ||
* Only opt out of this optimization if you know what you are doing. | ||
* | ||
* @defaultValue `true` | ||
*/ | ||
repositoryRequests?: boolean; | ||
/** | ||
* Determines if concurrent requests to the same URL should be optimized. The optimizations result in less network requests and quicker responses. | ||
* | ||
* Only opt out of this optimization if you know what you are doing. | ||
* | ||
* @defaultValue `true` | ||
*/ | ||
concurrentRequests?: boolean; | ||
}; | ||
}; | ||
/** | ||
* Parameters specific to client methods that fetch all documents. These methods | ||
@@ -269,2 +297,6 @@ * start with `getAll` (for example, `getAllByType`). | ||
/** | ||
* Options that determine if or how the client is optimized. | ||
*/ | ||
optimize: NonNullable<OptimizeParams["optimize"]>; | ||
/** | ||
* The client's ref mode state. This determines which ref is used during | ||
@@ -365,3 +397,3 @@ * queries. | ||
*/ | ||
get<TDocument extends TDocuments>(params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<Query<TDocument>>; | ||
get<TDocument extends TDocuments>(params?: Partial<BuildQueryURLArgs> & FetchParams & OptimizeParams): Promise<Query<TDocument>>; | ||
/** | ||
@@ -381,3 +413,3 @@ * Queries content from the Prismic repository and returns only the first | ||
*/ | ||
getFirst<TDocument extends TDocuments>(params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<TDocument>; | ||
getFirst<TDocument extends TDocuments>(params?: Partial<BuildQueryURLArgs> & FetchParams & OptimizeParams): Promise<TDocument>; | ||
/** | ||
@@ -405,3 +437,3 @@ * **IMPORTANT**: Avoid using `dangerouslyGetAll` as it may be slower and | ||
*/ | ||
dangerouslyGetAll<TDocument extends TDocuments>(params?: Partial<Omit<BuildQueryURLArgs, "page">> & GetAllParams & FetchParams): Promise<TDocument[]>; | ||
dangerouslyGetAll<TDocument extends TDocuments>(params?: Partial<Omit<BuildQueryURLArgs, "page">> & GetAllParams & FetchParams & OptimizeParams): Promise<TDocument[]>; | ||
/** | ||
@@ -428,3 +460,3 @@ * Queries a document from the Prismic repository with a specific ID. | ||
*/ | ||
getByID<TDocument extends TDocuments>(id: string, params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<TDocument>; | ||
getByID<TDocument extends TDocuments>(id: string, params?: Partial<BuildQueryURLArgs> & FetchParams & OptimizeParams): Promise<TDocument>; | ||
/** | ||
@@ -454,3 +486,3 @@ * Queries documents from the Prismic repository with specific IDs. | ||
*/ | ||
getByIDs<TDocument extends TDocuments>(ids: string[], params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<Query<TDocument>>; | ||
getByIDs<TDocument extends TDocuments>(ids: string[], params?: Partial<BuildQueryURLArgs> & FetchParams & OptimizeParams): Promise<Query<TDocument>>; | ||
/** | ||
@@ -482,3 +514,3 @@ * Queries all documents from the Prismic repository with specific IDs. | ||
*/ | ||
getAllByIDs<TDocument extends TDocuments>(ids: string[], params?: Partial<BuildQueryURLArgs> & GetAllParams & FetchParams): Promise<TDocument[]>; | ||
getAllByIDs<TDocument extends TDocuments>(ids: string[], params?: Partial<BuildQueryURLArgs> & GetAllParams & FetchParams & OptimizeParams): Promise<TDocument[]>; | ||
/** | ||
@@ -507,3 +539,3 @@ * Queries a document from the Prismic repository with a specific UID and | ||
*/ | ||
getByUID<TDocument extends TDocuments, TDocumentType extends TDocument["type"] = TDocument["type"]>(documentType: TDocumentType, uid: string, params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<ExtractDocumentType<TDocument, TDocumentType>>; | ||
getByUID<TDocument extends TDocuments, TDocumentType extends TDocument["type"] = TDocument["type"]>(documentType: TDocumentType, uid: string, params?: Partial<BuildQueryURLArgs> & FetchParams & OptimizeParams): Promise<ExtractDocumentType<TDocument, TDocumentType>>; | ||
/** | ||
@@ -535,3 +567,3 @@ * Queries document from the Prismic repository with specific UIDs and Custom | ||
*/ | ||
getByUIDs<TDocument extends TDocuments, TDocumentType extends TDocument["type"] = TDocument["type"]>(documentType: TDocumentType, uids: string[], params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<Query<ExtractDocumentType<TDocument, TDocumentType>>>; | ||
getByUIDs<TDocument extends TDocuments, TDocumentType extends TDocument["type"] = TDocument["type"]>(documentType: TDocumentType, uids: string[], params?: Partial<BuildQueryURLArgs> & FetchParams & OptimizeParams): Promise<Query<ExtractDocumentType<TDocument, TDocumentType>>>; | ||
/** | ||
@@ -565,3 +597,3 @@ * Queries all documents from the Prismic repository with specific UIDs and | ||
*/ | ||
getAllByUIDs<TDocument extends TDocuments, TDocumentType extends TDocument["type"] = TDocument["type"]>(documentType: TDocumentType, uids: string[], params?: Partial<BuildQueryURLArgs> & GetAllParams & FetchParams): Promise<ExtractDocumentType<TDocument, TDocumentType>[]>; | ||
getAllByUIDs<TDocument extends TDocuments, TDocumentType extends TDocument["type"] = TDocument["type"]>(documentType: TDocumentType, uids: string[], params?: Partial<BuildQueryURLArgs> & GetAllParams & FetchParams & OptimizeParams): Promise<ExtractDocumentType<TDocument, TDocumentType>[]>; | ||
/** | ||
@@ -589,3 +621,3 @@ * Queries a singleton document from the Prismic repository for a specific | ||
*/ | ||
getSingle<TDocument extends TDocuments, TDocumentType extends TDocument["type"] = TDocument["type"]>(documentType: TDocumentType, params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<ExtractDocumentType<TDocument, TDocumentType>>; | ||
getSingle<TDocument extends TDocuments, TDocumentType extends TDocument["type"] = TDocument["type"]>(documentType: TDocumentType, params?: Partial<BuildQueryURLArgs> & FetchParams & OptimizeParams): Promise<ExtractDocumentType<TDocument, TDocumentType>>; | ||
/** | ||
@@ -609,3 +641,3 @@ * Queries documents from the Prismic repository for a specific Custom Type. | ||
*/ | ||
getByType<TDocument extends TDocuments, TDocumentType extends TDocument["type"] = TDocument["type"]>(documentType: TDocumentType, params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<Query<ExtractDocumentType<TDocument, TDocumentType>>>; | ||
getByType<TDocument extends TDocuments, TDocumentType extends TDocument["type"] = TDocument["type"]>(documentType: TDocumentType, params?: Partial<BuildQueryURLArgs> & FetchParams & OptimizeParams): Promise<Query<ExtractDocumentType<TDocument, TDocumentType>>>; | ||
/** | ||
@@ -630,3 +662,3 @@ * Queries all documents from the Prismic repository for a specific Custom | ||
*/ | ||
getAllByType<TDocument extends TDocuments, TDocumentType extends TDocument["type"] = TDocument["type"]>(documentType: TDocumentType, params?: Partial<Omit<BuildQueryURLArgs, "page">> & GetAllParams & FetchParams): Promise<ExtractDocumentType<TDocument, TDocumentType>[]>; | ||
getAllByType<TDocument extends TDocuments, TDocumentType extends TDocument["type"] = TDocument["type"]>(documentType: TDocumentType, params?: Partial<Omit<BuildQueryURLArgs, "page">> & GetAllParams & FetchParams & OptimizeParams): Promise<ExtractDocumentType<TDocument, TDocumentType>[]>; | ||
/** | ||
@@ -650,3 +682,3 @@ * Queries documents from the Prismic repository with a specific tag. | ||
*/ | ||
getByTag<TDocument extends TDocuments>(tag: string, params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<Query<TDocument>>; | ||
getByTag<TDocument extends TDocuments>(tag: string, params?: Partial<BuildQueryURLArgs> & FetchParams & OptimizeParams): Promise<Query<TDocument>>; | ||
/** | ||
@@ -670,3 +702,3 @@ * Queries all documents from the Prismic repository with a specific tag. | ||
*/ | ||
getAllByTag<TDocument extends TDocuments>(tag: string, params?: Partial<Omit<BuildQueryURLArgs, "page">> & GetAllParams & FetchParams): Promise<TDocument[]>; | ||
getAllByTag<TDocument extends TDocuments>(tag: string, params?: Partial<Omit<BuildQueryURLArgs, "page">> & GetAllParams & FetchParams & OptimizeParams): Promise<TDocument[]>; | ||
/** | ||
@@ -688,3 +720,3 @@ * Queries documents from the Prismic repository with specific tags. A | ||
*/ | ||
getByEveryTag<TDocument extends TDocuments>(tags: string[], params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<Query<TDocument>>; | ||
getByEveryTag<TDocument extends TDocuments>(tags: string[], params?: Partial<BuildQueryURLArgs> & FetchParams & OptimizeParams): Promise<Query<TDocument>>; | ||
/** | ||
@@ -709,3 +741,3 @@ * Queries documents from the Prismic repository with specific tags. A | ||
*/ | ||
getAllByEveryTag<TDocument extends TDocuments>(tags: string[], params?: Partial<Omit<BuildQueryURLArgs, "page">> & GetAllParams & FetchParams): Promise<TDocument[]>; | ||
getAllByEveryTag<TDocument extends TDocuments>(tags: string[], params?: Partial<Omit<BuildQueryURLArgs, "page">> & GetAllParams & FetchParams & OptimizeParams): Promise<TDocument[]>; | ||
/** | ||
@@ -729,3 +761,3 @@ * Queries documents from the Prismic repository with specific tags. A | ||
*/ | ||
getBySomeTags<TDocument extends TDocuments>(tags: string[], params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<Query<TDocument>>; | ||
getBySomeTags<TDocument extends TDocuments>(tags: string[], params?: Partial<BuildQueryURLArgs> & FetchParams & OptimizeParams): Promise<Query<TDocument>>; | ||
/** | ||
@@ -751,3 +783,3 @@ * Queries documents from the Prismic repository with specific tags. A | ||
*/ | ||
getAllBySomeTags<TDocument extends TDocuments>(tags: string[], params?: Partial<Omit<BuildQueryURLArgs, "page">> & GetAllParams & FetchParams): Promise<TDocument[]>; | ||
getAllBySomeTags<TDocument extends TDocuments>(tags: string[], params?: Partial<Omit<BuildQueryURLArgs, "page">> & GetAllParams & FetchParams & OptimizeParams): Promise<TDocument[]>; | ||
/** | ||
@@ -759,3 +791,3 @@ * Returns metadata about the Prismic repository, such as its refs, releases, | ||
*/ | ||
getRepository(params?: FetchParams): Promise<Repository>; | ||
getRepository(params?: FetchParams & OptimizeParams): Promise<Repository>; | ||
/** | ||
@@ -770,3 +802,3 @@ * Returns a list of all refs for the Prismic repository. | ||
*/ | ||
getRefs(params?: FetchParams): Promise<Ref[]>; | ||
getRefs(params?: FetchParams & OptimizeParams): Promise<Ref[]>; | ||
/** | ||
@@ -779,3 +811,3 @@ * Returns a ref for the Prismic repository with a matching ID. | ||
*/ | ||
getRefByID(id: string, params?: FetchParams): Promise<Ref>; | ||
getRefByID(id: string, params?: FetchParams & OptimizeParams): Promise<Ref>; | ||
/** | ||
@@ -788,3 +820,3 @@ * Returns a ref for the Prismic repository with a matching label. | ||
*/ | ||
getRefByLabel(label: string, params?: FetchParams): Promise<Ref>; | ||
getRefByLabel(label: string, params?: FetchParams & OptimizeParams): Promise<Ref>; | ||
/** | ||
@@ -796,3 +828,3 @@ * Returns the master ref for the Prismic repository. The master ref points to | ||
*/ | ||
getMasterRef(params?: FetchParams): Promise<Ref>; | ||
getMasterRef(params?: FetchParams & OptimizeParams): Promise<Ref>; | ||
/** | ||
@@ -804,3 +836,3 @@ * Returns a list of all Releases for the Prismic repository. Releases are | ||
*/ | ||
getReleases(params?: FetchParams): Promise<Ref[]>; | ||
getReleases(params?: FetchParams & OptimizeParams): Promise<Ref[]>; | ||
/** | ||
@@ -813,3 +845,3 @@ * Returns a Release for the Prismic repository with a matching ID. | ||
*/ | ||
getReleaseByID(id: string, params?: FetchParams): Promise<Ref>; | ||
getReleaseByID(id: string, params?: FetchParams & OptimizeParams): Promise<Ref>; | ||
/** | ||
@@ -822,3 +854,3 @@ * Returns a Release for the Prismic repository with a matching label. | ||
*/ | ||
getReleaseByLabel(label: string, params?: FetchParams): Promise<Ref>; | ||
getReleaseByLabel(label: string, params?: FetchParams & OptimizeParams): Promise<Ref>; | ||
/** | ||
@@ -829,3 +861,3 @@ * Returns a list of all tags used in the Prismic repository. | ||
*/ | ||
getTags(params?: FetchParams): Promise<string[]>; | ||
getTags(params?: FetchParams & OptimizeParams): Promise<string[]>; | ||
/** | ||
@@ -838,3 +870,3 @@ * Builds a URL used to query content from the Prismic repository. | ||
*/ | ||
buildQueryURL({ signal, ...params }?: Partial<BuildQueryURLArgs> & FetchParams): Promise<string>; | ||
buildQueryURL({ signal, optimize, ...params }?: Partial<BuildQueryURLArgs> & FetchParams & OptimizeParams): Promise<string>; | ||
/** | ||
@@ -859,3 +891,3 @@ * Determines the URL for a previewed document during an active preview | ||
*/ | ||
resolvePreviewURL<LinkResolverReturnType>(args: ResolvePreviewArgs<LinkResolverReturnType> & FetchParams): Promise<string>; | ||
resolvePreviewURL<LinkResolverReturnType>(args: ResolvePreviewArgs<LinkResolverReturnType> & FetchParams & OptimizeParams): Promise<string>; | ||
/** | ||
@@ -862,0 +894,0 @@ * Configures the client to query the latest published content for all future |
@@ -29,2 +29,3 @@ var __defProp = Object.defineProperty; | ||
const GET_ALL_QUERY_DELAY = 500; | ||
const OPTIMIZE_REPOSITORY_REQUESTS_VALID_UNTIL_PARAMETER_NAME = "x-valid-until"; | ||
var RefStateMode; | ||
@@ -94,2 +95,6 @@ (function(RefStateMode2) { | ||
/** | ||
* Options that determine if or how the client is optimized. | ||
*/ | ||
__publicField(this, "optimize"); | ||
/** | ||
* The client's ref mode state. This determines which ref is used during | ||
@@ -114,2 +119,3 @@ * queries. | ||
__publicField(this, "fetchJobs", {}); | ||
var _a, _b; | ||
if (isRepositoryEndpoint(repositoryNameOrEndpoint)) { | ||
@@ -127,2 +133,6 @@ if (process.env.NODE_ENV === "development" && /\.prismic\.io\/(?!api\/v2\/?)/.test(repositoryNameOrEndpoint)) { | ||
this.defaultParams = options.defaultParams; | ||
this.optimize = { | ||
repositoryRequests: ((_a = options.optimize) == null ? void 0 : _a.repositoryRequests) ?? true, | ||
concurrentRequests: ((_b = options.optimize) == null ? void 0 : _b.concurrentRequests) ?? true | ||
}; | ||
if (options.ref) { | ||
@@ -233,6 +243,6 @@ this.queryContentFromRef(options.ref); | ||
async getFirst(params) { | ||
var _a, _b; | ||
var _a; | ||
const actualParams = { ...params }; | ||
if (!(params && params.page) && !(params == null ? void 0 : params.pageSize)) { | ||
actualParams.pageSize = ((_a = this.defaultParams) == null ? void 0 : _a.pageSize) == null ? 1 : (_b = this.defaultParams) == null ? void 0 : _b.pageSize; | ||
actualParams.pageSize = ((_a = this.defaultParams) == null ? void 0 : _a.pageSize) ?? 1; | ||
} | ||
@@ -665,2 +675,3 @@ const url = await this.buildQueryURL(actualParams); | ||
async getRepository(params) { | ||
var _a; | ||
const url = new URL(this.endpoint); | ||
@@ -670,2 +681,5 @@ if (this.accessToken) { | ||
} | ||
if (((_a = params == null ? void 0 : params.optimize) == null ? void 0 : _a.repositoryRequests) ?? this.optimize.repositoryRequests) { | ||
url.searchParams.set(OPTIMIZE_REPOSITORY_REQUESTS_VALID_UNTIL_PARAMETER_NAME, (Math.floor(Date.now() / REPOSITORY_CACHE_TTL) * REPOSITORY_CACHE_TTL + REPOSITORY_CACHE_TTL).toString()); | ||
} | ||
return await this.fetch(url.toString(), params); | ||
@@ -775,5 +789,5 @@ } | ||
*/ | ||
async buildQueryURL({ signal, ...params } = {}) { | ||
const ref = params.ref || await this.getResolvedRefString(); | ||
const integrationFieldsRef = params.integrationFieldsRef || (await this.getCachedRepository({ signal })).integrationFieldsRef || void 0; | ||
async buildQueryURL({ signal, optimize, ...params } = {}) { | ||
const ref = params.ref || await this.getResolvedRefString({ signal, optimize }); | ||
const integrationFieldsRef = params.integrationFieldsRef || (await this.getCachedRepository({ signal, optimize })).integrationFieldsRef || void 0; | ||
return buildQueryURL(this.endpoint, { | ||
@@ -828,2 +842,3 @@ ...this.defaultParams, | ||
const document = await this.getByID(documentID, { | ||
optimize: args.optimize, | ||
signal: args.signal, | ||
@@ -1005,7 +1020,12 @@ ref: previewToken, | ||
async getCachedRepository(params) { | ||
if (!this.cachedRepository || Date.now() >= this.cachedRepositoryExpiration) { | ||
this.cachedRepositoryExpiration = Date.now() + REPOSITORY_CACHE_TTL; | ||
this.cachedRepository = await this.getRepository(params); | ||
var _a; | ||
if (((_a = params == null ? void 0 : params.optimize) == null ? void 0 : _a.repositoryRequests) ?? this.optimize.repositoryRequests) { | ||
if (!this.cachedRepository || Date.now() >= this.cachedRepositoryExpiration) { | ||
this.cachedRepositoryExpiration = Date.now() + REPOSITORY_CACHE_TTL; | ||
this.cachedRepository = await this.getRepository(params); | ||
} | ||
return this.cachedRepository; | ||
} else { | ||
return await this.getRepository(params); | ||
} | ||
return this.cachedRepository; | ||
} | ||
@@ -1099,28 +1119,48 @@ /** | ||
async fetch(url, params = {}) { | ||
let job; | ||
if (this.fetchJobs[url] && this.fetchJobs[url].has(params.signal)) { | ||
job = this.fetchJobs[url].get(params.signal); | ||
var _a; | ||
let res; | ||
if (((_a = params.optimize) == null ? void 0 : _a.concurrentRequests) ?? this.optimize.concurrentRequests) { | ||
let job; | ||
const fetchJobKeyInstance = new URL(url); | ||
fetchJobKeyInstance.searchParams.delete(OPTIMIZE_REPOSITORY_REQUESTS_VALID_UNTIL_PARAMETER_NAME); | ||
const fetchJobKey = fetchJobKeyInstance.toString(); | ||
if (this.fetchJobs[fetchJobKey] && this.fetchJobs[fetchJobKey].has(params.signal)) { | ||
job = this.fetchJobs[fetchJobKey].get(params.signal); | ||
} else { | ||
this.fetchJobs[fetchJobKey] = this.fetchJobs[fetchJobKey] || /* @__PURE__ */ new Map(); | ||
job = this.fetchFn(url, { | ||
signal: params.signal | ||
}).then(async (res2) => { | ||
let json = void 0; | ||
try { | ||
json = await res2.json(); | ||
} catch { | ||
} | ||
return { | ||
status: res2.status, | ||
json | ||
}; | ||
}).finally(() => { | ||
this.fetchJobs[fetchJobKey].delete(params.signal); | ||
if (this.fetchJobs[fetchJobKey].size === 0) { | ||
delete this.fetchJobs[fetchJobKey]; | ||
} | ||
}); | ||
this.fetchJobs[fetchJobKey].set(params.signal, job); | ||
} | ||
res = await job; | ||
} else { | ||
this.fetchJobs[url] = this.fetchJobs[url] || /* @__PURE__ */ new Map(); | ||
job = this.fetchFn(url, { | ||
const job = await this.fetchFn(url, { | ||
signal: params.signal | ||
}).then(async (res2) => { | ||
let json = void 0; | ||
try { | ||
json = await res2.json(); | ||
} catch { | ||
} | ||
return { | ||
status: res2.status, | ||
json | ||
}; | ||
}).finally(() => { | ||
this.fetchJobs[url].delete(params.signal); | ||
if (this.fetchJobs[url].size === 0) { | ||
delete this.fetchJobs[url]; | ||
} | ||
}); | ||
this.fetchJobs[url].set(params.signal, job); | ||
let json = void 0; | ||
try { | ||
json = await job.json(); | ||
} catch { | ||
} | ||
res = { | ||
status: job.status, | ||
json | ||
}; | ||
} | ||
const res = await job; | ||
if (res.status !== 404 && res.json == null) { | ||
@@ -1127,0 +1167,0 @@ throw new PrismicError(void 0, url, res.json); |
@@ -1,2 +0,2 @@ | ||
const version = "7.0.0-alpha.2"; | ||
const version = "7.0.0-alpha.3"; | ||
export { | ||
@@ -3,0 +3,0 @@ version |
{ | ||
"name": "@prismicio/client", | ||
"version": "7.0.0-alpha.2", | ||
"version": "7.0.0-alpha.3", | ||
"description": "The official JavaScript + TypeScript client library for Prismic", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -53,2 +53,9 @@ import { appendFilters } from "./lib/appendFilters"; | ||
/** | ||
* The URL search parameter name used for optimizing `/api/v2` requests. The | ||
* value of the parameter effectively forcibly cache-busts `/api/v2` requests | ||
* after REPOSITORY_CACHE_TTL milliseconds have elapsed. | ||
*/ | ||
const OPTIMIZE_REPOSITORY_REQUESTS_VALID_UNTIL_PARAMETER_NAME = "x-valid-until"; | ||
/** | ||
* Extracts one or more Prismic document types that match a given Prismic | ||
@@ -261,3 +268,3 @@ * document type. If no matches are found, no extraction is performed and the | ||
fetch?: FetchLike; | ||
}; | ||
} & OptimizeParams; | ||
@@ -279,2 +286,32 @@ /** | ||
/** | ||
* Parameters that determine if or how the client is optimized. | ||
*/ | ||
type OptimizeParams = { | ||
/** | ||
* Options that determine if or how the client is optimized. | ||
*/ | ||
optimize?: { | ||
/** | ||
* Determines if fetching repository metadata should be optimized. The | ||
* optimizations result in faster queries, less network requests, and more | ||
* accurate cache-busting. | ||
* | ||
* Only opt out of this optimization if you know what you are doing. | ||
* | ||
* @defaultValue `true` | ||
*/ | ||
repositoryRequests?: boolean; | ||
/** | ||
* Determines if concurrent requests to the same URL should be optimized. The optimizations result in less network requests and quicker responses. | ||
* | ||
* Only opt out of this optimization if you know what you are doing. | ||
* | ||
* @defaultValue `true` | ||
*/ | ||
concurrentRequests?: boolean; | ||
}; | ||
}; | ||
/** | ||
* Parameters specific to client methods that fetch all documents. These methods | ||
@@ -423,2 +460,7 @@ * start with `getAll` (for example, `getAllByType`). | ||
/** | ||
* Options that determine if or how the client is optimized. | ||
*/ | ||
optimize: NonNullable<OptimizeParams["optimize"]>; | ||
/** | ||
* The client's ref mode state. This determines which ref is used during | ||
@@ -486,2 +528,6 @@ * queries. | ||
this.defaultParams = options.defaultParams; | ||
this.optimize = { | ||
repositoryRequests: options.optimize?.repositoryRequests ?? true, | ||
concurrentRequests: options.optimize?.concurrentRequests ?? true, | ||
}; | ||
@@ -587,3 +633,3 @@ if (options.ref) { | ||
async get<TDocument extends TDocuments>( | ||
params?: Partial<BuildQueryURLArgs> & FetchParams, | ||
params?: Partial<BuildQueryURLArgs> & FetchParams & OptimizeParams, | ||
): Promise<Query<TDocument>> { | ||
@@ -610,8 +656,7 @@ const url = await this.buildQueryURL(params); | ||
async getFirst<TDocument extends TDocuments>( | ||
params?: Partial<BuildQueryURLArgs> & FetchParams, | ||
params?: Partial<BuildQueryURLArgs> & FetchParams & OptimizeParams, | ||
): Promise<TDocument> { | ||
const actualParams = { ...params }; | ||
if (!(params && params.page) && !params?.pageSize) { | ||
actualParams.pageSize = | ||
this.defaultParams?.pageSize == null ? 1 : this.defaultParams?.pageSize; | ||
actualParams.pageSize = this.defaultParams?.pageSize ?? 1; | ||
} | ||
@@ -655,3 +700,4 @@ const url = await this.buildQueryURL(actualParams); | ||
GetAllParams & | ||
FetchParams = {}, | ||
FetchParams & | ||
OptimizeParams = {}, | ||
): Promise<TDocument[]> { | ||
@@ -710,3 +756,3 @@ const { limit = Infinity, ...actualParams } = params; | ||
id: string, | ||
params?: Partial<BuildQueryURLArgs> & FetchParams, | ||
params?: Partial<BuildQueryURLArgs> & FetchParams & OptimizeParams, | ||
): Promise<TDocument> { | ||
@@ -744,3 +790,3 @@ return await this.getFirst<TDocument>( | ||
ids: string[], | ||
params?: Partial<BuildQueryURLArgs> & FetchParams, | ||
params?: Partial<BuildQueryURLArgs> & FetchParams & OptimizeParams, | ||
): Promise<Query<TDocument>> { | ||
@@ -780,3 +826,6 @@ return await this.get<TDocument>( | ||
ids: string[], | ||
params?: Partial<BuildQueryURLArgs> & GetAllParams & FetchParams, | ||
params?: Partial<BuildQueryURLArgs> & | ||
GetAllParams & | ||
FetchParams & | ||
OptimizeParams, | ||
): Promise<TDocument[]> { | ||
@@ -817,3 +866,3 @@ return await this.dangerouslyGetAll<TDocument>( | ||
uid: string, | ||
params?: Partial<BuildQueryURLArgs> & FetchParams, | ||
params?: Partial<BuildQueryURLArgs> & FetchParams & OptimizeParams, | ||
): Promise<ExtractDocumentType<TDocument, TDocumentType>> { | ||
@@ -860,3 +909,3 @@ return await this.getFirst<ExtractDocumentType<TDocument, TDocumentType>>( | ||
uids: string[], | ||
params?: Partial<BuildQueryURLArgs> & FetchParams, | ||
params?: Partial<BuildQueryURLArgs> & FetchParams & OptimizeParams, | ||
): Promise<Query<ExtractDocumentType<TDocument, TDocumentType>>> { | ||
@@ -905,3 +954,6 @@ return await this.get<ExtractDocumentType<TDocument, TDocumentType>>( | ||
uids: string[], | ||
params?: Partial<BuildQueryURLArgs> & GetAllParams & FetchParams, | ||
params?: Partial<BuildQueryURLArgs> & | ||
GetAllParams & | ||
FetchParams & | ||
OptimizeParams, | ||
): Promise<ExtractDocumentType<TDocument, TDocumentType>[]> { | ||
@@ -945,3 +997,3 @@ return await this.dangerouslyGetAll< | ||
documentType: TDocumentType, | ||
params?: Partial<BuildQueryURLArgs> & FetchParams, | ||
params?: Partial<BuildQueryURLArgs> & FetchParams & OptimizeParams, | ||
): Promise<ExtractDocumentType<TDocument, TDocumentType>> { | ||
@@ -976,3 +1028,3 @@ return await this.getFirst<ExtractDocumentType<TDocument, TDocumentType>>( | ||
documentType: TDocumentType, | ||
params?: Partial<BuildQueryURLArgs> & FetchParams, | ||
params?: Partial<BuildQueryURLArgs> & FetchParams & OptimizeParams, | ||
): Promise<Query<ExtractDocumentType<TDocument, TDocumentType>>> { | ||
@@ -1010,3 +1062,4 @@ return await this.get<ExtractDocumentType<TDocument, TDocumentType>>( | ||
GetAllParams & | ||
FetchParams, | ||
FetchParams & | ||
OptimizeParams, | ||
): Promise<ExtractDocumentType<TDocument, TDocumentType>[]> { | ||
@@ -1038,3 +1091,3 @@ return await this.dangerouslyGetAll< | ||
tag: string, | ||
params?: Partial<BuildQueryURLArgs> & FetchParams, | ||
params?: Partial<BuildQueryURLArgs> & FetchParams & OptimizeParams, | ||
): Promise<Query<TDocument>> { | ||
@@ -1068,3 +1121,4 @@ return await this.get<TDocument>( | ||
GetAllParams & | ||
FetchParams, | ||
FetchParams & | ||
OptimizeParams, | ||
): Promise<TDocument[]> { | ||
@@ -1094,3 +1148,3 @@ return await this.dangerouslyGetAll<TDocument>( | ||
tags: string[], | ||
params?: Partial<BuildQueryURLArgs> & FetchParams, | ||
params?: Partial<BuildQueryURLArgs> & FetchParams & OptimizeParams, | ||
): Promise<Query<TDocument>> { | ||
@@ -1125,3 +1179,4 @@ return await this.get<TDocument>( | ||
GetAllParams & | ||
FetchParams, | ||
FetchParams & | ||
OptimizeParams, | ||
): Promise<TDocument[]> { | ||
@@ -1153,3 +1208,3 @@ return await this.dangerouslyGetAll<TDocument>( | ||
tags: string[], | ||
params?: Partial<BuildQueryURLArgs> & FetchParams, | ||
params?: Partial<BuildQueryURLArgs> & FetchParams & OptimizeParams, | ||
): Promise<Query<TDocument>> { | ||
@@ -1185,3 +1240,4 @@ return await this.get<TDocument>( | ||
GetAllParams & | ||
FetchParams, | ||
FetchParams & | ||
OptimizeParams, | ||
): Promise<TDocument[]> { | ||
@@ -1199,3 +1255,5 @@ return await this.dangerouslyGetAll<TDocument>( | ||
*/ | ||
async getRepository(params?: FetchParams): Promise<Repository> { | ||
async getRepository( | ||
params?: FetchParams & OptimizeParams, | ||
): Promise<Repository> { | ||
// TODO: Restore when Authorization header support works in browsers with CORS. | ||
@@ -1210,2 +1268,24 @@ // return await this.fetch<Repository>(this.endpoint); | ||
if ( | ||
params?.optimize?.repositoryRequests ?? | ||
this.optimize.repositoryRequests | ||
) { | ||
// Add a URL parameter to fingerprint the URL. The URL | ||
// should be valid for the duration of | ||
// REPOSITORY_CACHE_TTL. | ||
// | ||
// This fingerprint works as a second "cache" that | ||
// works outside `getRepositoryCache()`. This is useful | ||
// when multiple client instances exist with an | ||
// external cache; all clients can share the same URL | ||
// cache key for the duration of REPOSITORY_CACHE_TTL. | ||
url.searchParams.set( | ||
OPTIMIZE_REPOSITORY_REQUESTS_VALID_UNTIL_PARAMETER_NAME, | ||
( | ||
Math.floor(Date.now() / REPOSITORY_CACHE_TTL) * REPOSITORY_CACHE_TTL + | ||
REPOSITORY_CACHE_TTL | ||
).toString(), | ||
); | ||
} | ||
return await this.fetch<Repository>(url.toString(), params); | ||
@@ -1223,3 +1303,3 @@ } | ||
*/ | ||
async getRefs(params?: FetchParams): Promise<Ref[]> { | ||
async getRefs(params?: FetchParams & OptimizeParams): Promise<Ref[]> { | ||
const repository = await this.getRepository(params); | ||
@@ -1237,3 +1317,6 @@ | ||
*/ | ||
async getRefByID(id: string, params?: FetchParams): Promise<Ref> { | ||
async getRefByID( | ||
id: string, | ||
params?: FetchParams & OptimizeParams, | ||
): Promise<Ref> { | ||
const refs = await this.getRefs(params); | ||
@@ -1251,3 +1334,6 @@ | ||
*/ | ||
async getRefByLabel(label: string, params?: FetchParams): Promise<Ref> { | ||
async getRefByLabel( | ||
label: string, | ||
params?: FetchParams & OptimizeParams, | ||
): Promise<Ref> { | ||
const refs = await this.getRefs(params); | ||
@@ -1264,3 +1350,3 @@ | ||
*/ | ||
async getMasterRef(params?: FetchParams): Promise<Ref> { | ||
async getMasterRef(params?: FetchParams & OptimizeParams): Promise<Ref> { | ||
const refs = await this.getRefs(params); | ||
@@ -1277,3 +1363,3 @@ | ||
*/ | ||
async getReleases(params?: FetchParams): Promise<Ref[]> { | ||
async getReleases(params?: FetchParams & OptimizeParams): Promise<Ref[]> { | ||
const refs = await this.getRefs(params); | ||
@@ -1291,3 +1377,6 @@ | ||
*/ | ||
async getReleaseByID(id: string, params?: FetchParams): Promise<Ref> { | ||
async getReleaseByID( | ||
id: string, | ||
params?: FetchParams & OptimizeParams, | ||
): Promise<Ref> { | ||
const releases = await this.getReleases(params); | ||
@@ -1305,3 +1394,6 @@ | ||
*/ | ||
async getReleaseByLabel(label: string, params?: FetchParams): Promise<Ref> { | ||
async getReleaseByLabel( | ||
label: string, | ||
params?: FetchParams & OptimizeParams, | ||
): Promise<Ref> { | ||
const releases = await this.getReleases(params); | ||
@@ -1317,3 +1409,3 @@ | ||
*/ | ||
async getTags(params?: FetchParams): Promise<string[]> { | ||
async getTags(params?: FetchParams & OptimizeParams): Promise<string[]> { | ||
try { | ||
@@ -1345,8 +1437,13 @@ const tagsForm = await this.getCachedRepositoryForm("tags", params); | ||
signal, | ||
optimize, | ||
...params | ||
}: Partial<BuildQueryURLArgs> & FetchParams = {}): Promise<string> { | ||
const ref = params.ref || (await this.getResolvedRefString()); | ||
}: Partial<BuildQueryURLArgs> & | ||
FetchParams & | ||
OptimizeParams = {}): Promise<string> { | ||
const ref = | ||
params.ref || (await this.getResolvedRefString({ signal, optimize })); | ||
const integrationFieldsRef = | ||
params.integrationFieldsRef || | ||
(await this.getCachedRepository({ signal })).integrationFieldsRef || | ||
(await this.getCachedRepository({ signal, optimize })) | ||
.integrationFieldsRef || | ||
undefined; | ||
@@ -1385,3 +1482,5 @@ | ||
async resolvePreviewURL<LinkResolverReturnType>( | ||
args: ResolvePreviewArgs<LinkResolverReturnType> & FetchParams, | ||
args: ResolvePreviewArgs<LinkResolverReturnType> & | ||
FetchParams & | ||
OptimizeParams, | ||
): Promise<string> { | ||
@@ -1416,2 +1515,3 @@ let documentID: string | undefined | null = args.documentID; | ||
const document = await this.getByID(documentID, { | ||
optimize: args.optimize, | ||
signal: args.signal, | ||
@@ -1624,12 +1724,21 @@ ref: previewToken, | ||
*/ | ||
private async getCachedRepository(params?: FetchParams): Promise<Repository> { | ||
private async getCachedRepository( | ||
params?: FetchParams & OptimizeParams, | ||
): Promise<Repository> { | ||
if ( | ||
!this.cachedRepository || | ||
Date.now() >= this.cachedRepositoryExpiration | ||
params?.optimize?.repositoryRequests ?? | ||
this.optimize.repositoryRequests | ||
) { | ||
this.cachedRepositoryExpiration = Date.now() + REPOSITORY_CACHE_TTL; | ||
this.cachedRepository = await this.getRepository(params); | ||
if ( | ||
!this.cachedRepository || | ||
Date.now() >= this.cachedRepositoryExpiration | ||
) { | ||
this.cachedRepositoryExpiration = Date.now() + REPOSITORY_CACHE_TTL; | ||
this.cachedRepository = await this.getRepository(params); | ||
} | ||
return this.cachedRepository; | ||
} else { | ||
return await this.getRepository(params); | ||
} | ||
return this.cachedRepository; | ||
} | ||
@@ -1648,3 +1757,3 @@ | ||
name: string, | ||
params?: FetchParams, | ||
params?: FetchParams & OptimizeParams, | ||
): Promise<Form> { | ||
@@ -1688,3 +1797,5 @@ const cachedRepository = await this.getCachedRepository(params); | ||
*/ | ||
private async getResolvedRefString(params?: FetchParams): Promise<string> { | ||
private async getResolvedRefString( | ||
params?: FetchParams & OptimizeParams, | ||
): Promise<string> { | ||
if (this.refState.autoPreviewsEnabled) { | ||
@@ -1751,52 +1862,90 @@ let previewRef: string | undefined; | ||
url: string, | ||
params: FetchParams = {}, | ||
params: FetchParams & OptimizeParams = {}, | ||
): Promise<T> { | ||
let job: Promise<FetchJobResult>; | ||
let res: FetchJobResult; | ||
// `fetchJobs` is keyed twice: first by the URL and again by is | ||
// signal, if one exists. | ||
// | ||
// Using two keys allows us to reuse fetch requests for | ||
// equivalent URLs, but eject when we detect unique signals. | ||
if (this.fetchJobs[url] && this.fetchJobs[url].has(params.signal)) { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
job = this.fetchJobs[url].get(params.signal)!; | ||
} else { | ||
this.fetchJobs[url] = this.fetchJobs[url] || new Map(); | ||
if ( | ||
params.optimize?.concurrentRequests ?? | ||
this.optimize.concurrentRequests | ||
) { | ||
let job: Promise<FetchJobResult>; | ||
job = this.fetchFn(url, { | ||
signal: params.signal, | ||
}) | ||
.then(async (res) => { | ||
// We can assume Prismic REST API responses | ||
// will have a `application/json` | ||
// Content Type. If not, this will | ||
// throw, signaling an invalid | ||
// response. | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
let json: any = undefined; | ||
try { | ||
json = await res.json(); | ||
} catch { | ||
// noop | ||
} | ||
const fetchJobKeyInstance = new URL(url); | ||
fetchJobKeyInstance.searchParams.delete( | ||
OPTIMIZE_REPOSITORY_REQUESTS_VALID_UNTIL_PARAMETER_NAME, | ||
); | ||
const fetchJobKey = fetchJobKeyInstance.toString(); | ||
return { | ||
status: res.status, | ||
json, | ||
}; | ||
// `fetchJobs` is keyed twice: first by the URL and again by is | ||
// signal, if one exists. | ||
// | ||
// Using two keys allows us to reuse fetch requests for | ||
// equivalent URLs, but eject when we detect unique signals. | ||
if ( | ||
this.fetchJobs[fetchJobKey] && | ||
this.fetchJobs[fetchJobKey].has(params.signal) | ||
) { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
job = this.fetchJobs[fetchJobKey].get(params.signal)!; | ||
} else { | ||
this.fetchJobs[fetchJobKey] = this.fetchJobs[fetchJobKey] || new Map(); | ||
job = this.fetchFn(url, { | ||
signal: params.signal, | ||
}) | ||
.finally(() => { | ||
this.fetchJobs[url].delete(params.signal); | ||
.then(async (res) => { | ||
// We can assume Prismic REST API responses | ||
// will have a `application/json` | ||
// Content Type. If not, this will | ||
// throw, signaling an invalid | ||
// response. | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
let json: any = undefined; | ||
try { | ||
json = await res.json(); | ||
} catch { | ||
// noop | ||
} | ||
if (this.fetchJobs[url].size === 0) { | ||
delete this.fetchJobs[url]; | ||
} | ||
}); | ||
return { | ||
status: res.status, | ||
json, | ||
}; | ||
}) | ||
.finally(() => { | ||
this.fetchJobs[fetchJobKey].delete(params.signal); | ||
this.fetchJobs[url].set(params.signal, job); | ||
if (this.fetchJobs[fetchJobKey].size === 0) { | ||
delete this.fetchJobs[fetchJobKey]; | ||
} | ||
}); | ||
this.fetchJobs[fetchJobKey].set(params.signal, job); | ||
} | ||
res = await job; | ||
} else { | ||
const job = await this.fetchFn(url, { | ||
signal: params.signal, | ||
}); | ||
// We can assume Prismic REST API responses | ||
// will have a `application/json` | ||
// Content Type. If not, this will | ||
// throw, signaling an invalid | ||
// response. | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
let json: any = undefined; | ||
try { | ||
json = await job.json(); | ||
} catch { | ||
// noop | ||
} | ||
res = { | ||
status: job.status, | ||
json, | ||
}; | ||
} | ||
const res = await job; | ||
if (res.status !== 404 && res.json == null) { | ||
@@ -1803,0 +1952,0 @@ throw new PrismicError(undefined, url, res.json); |
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
Sorry, the diff of this file is not supported yet
950806
14947