@prismicio/custom-types-client
Advanced tools
Comparing version 1.2.0-alpha.0 to 1.2.0
import type * as prismicT from "@prismicio/types"; | ||
import type { AbortSignalLike, FetchLike } from "./types"; | ||
import type { AbortSignalLike, FetchLike, RequestInitLike } from "./types"; | ||
/** | ||
@@ -28,6 +28,7 @@ * Configuration for creating a `CustomTypesClient`. | ||
/** | ||
* The "User-Agent" header used in all outgoing requests to the Custom Type | ||
* API. It's used by the Custom Type API for tracking purposes. | ||
* Options provided to the client's `fetch()` on all network requests. These | ||
* options will be merged with internally required options. They can also be | ||
* overriden on a per-query basis using the query's `fetchOptions` parameter. | ||
*/ | ||
userAgent?: string; | ||
fetchOptions?: RequestInitLike; | ||
}; | ||
@@ -38,9 +39,14 @@ /** | ||
*/ | ||
export type CustomTypesClientMethodParams = Partial<Pick<CustomTypesClientConfig, "repositoryName" | "endpoint" | "token" | "userAgent">>; | ||
export type CustomTypesClientMethodParams = Partial<Pick<CustomTypesClientConfig, "repositoryName" | "endpoint" | "token">>; | ||
/** | ||
* Parameters for any client method that use `fetch()`. Only a subset of | ||
* `fetch()` parameters are exposed. | ||
* Parameters for client methods that use `fetch()`. | ||
*/ | ||
type FetchParams = { | ||
/** | ||
* Options provided to the client's `fetch()` on all network requests. These | ||
* options will be merged with internally required options. They can also be | ||
* overriden on a per-query basis using the query's `fetchOptions` parameter. | ||
*/ | ||
fetchOptions?: RequestInitLike; | ||
/** | ||
* An `AbortSignal` provided by an `AbortController`. This allows the network | ||
@@ -86,6 +92,7 @@ * request to be cancelled if necessary. | ||
/** | ||
* The "User-Agent" header used in all outgoing requests to the Custom Type | ||
* API. It's used by the Custom Type API for tracking purposes. | ||
* Options provided to the client's `fetch()` on all network requests. These | ||
* options will be merged with internally required options. They can also be | ||
* overriden on a per-query basis using the query's `fetchOptions` parameter. | ||
*/ | ||
userAgent?: string; | ||
fetchOptions?: RequestInitLike; | ||
/** | ||
@@ -92,0 +99,0 @@ * Create a client for the Prismic Custom Types API. |
@@ -22,7 +22,7 @@ var __defProp = Object.defineProperty; | ||
__publicField(this, "fetchFn"); | ||
__publicField(this, "userAgent"); | ||
__publicField(this, "fetchOptions"); | ||
this.repositoryName = config.repositoryName; | ||
this.endpoint = config.endpoint || DEFAULT_CUSTOM_TYPES_API_ENDPOINT; | ||
this.token = config.token; | ||
this.userAgent = config.userAgent; | ||
this.fetchOptions = config.fetchOptions; | ||
if (/\/customtypes\/?$/.test(this.endpoint)) { | ||
@@ -83,6 +83,9 @@ this.endpoint = this.endpoint.replace(/\/customtypes\/?$/, ""); | ||
async fetch(path, params = {}, requestInit = {}) { | ||
var _a, _b, _c, _d; | ||
const endpoint = params.endpoint || this.endpoint; | ||
const url = new URL(path, endpoint.endsWith("/") ? endpoint : `${endpoint}/`).toString(); | ||
const userAgent = params.userAgent || this.userAgent; | ||
const res = await this.fetchFn(url, { | ||
...this.fetchOptions, | ||
...requestInit, | ||
...params.fetchOptions, | ||
headers: { | ||
@@ -92,6 +95,7 @@ "Content-Type": "application/json", | ||
Authorization: `Bearer ${params.token || this.token}`, | ||
...userAgent && { "User-Agent": userAgent } | ||
...(_a = this.fetchOptions) == null ? void 0 : _a.headers, | ||
...requestInit.headers, | ||
...(_b = params.fetchOptions) == null ? void 0 : _b.headers | ||
}, | ||
signal: params.signal, | ||
...requestInit | ||
signal: ((_c = params.fetchOptions) == null ? void 0 : _c.signal) || params.signal || requestInit.signal || ((_d = this.fetchOptions) == null ? void 0 : _d.signal) | ||
}); | ||
@@ -98,0 +102,0 @@ switch (res.status) { |
@@ -0,0 +0,0 @@ /** |
@@ -0,0 +0,0 @@ export { createClient, CustomTypesClient } from "./client"; |
@@ -13,8 +13,17 @@ /** | ||
/** | ||
* The minimum required properties from RequestInit. | ||
* A subset of RequestInit properties to configure a `fetch()` request. | ||
*/ | ||
export interface RequestInitLike { | ||
export interface RequestInitLike extends Partial<Pick<RequestInit, "cache">> { | ||
method?: "GET" | "POST" | "DELETE"; | ||
body?: string; | ||
/** | ||
* An object literal to set the `fetch()` request's headers. | ||
*/ | ||
headers?: Record<string, string>; | ||
/** | ||
* An AbortSignal to set the `fetch()` request's signal. | ||
* | ||
* See: | ||
* [https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) | ||
*/ | ||
signal?: AbortSignalLike; | ||
@@ -21,0 +30,0 @@ } |
{ | ||
"name": "@prismicio/custom-types-client", | ||
"version": "1.2.0-alpha.0", | ||
"version": "1.2.0", | ||
"description": "JavaScript client to interact with the Prismic Custom Types API", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -48,6 +48,7 @@ import type * as prismicT from "@prismicio/types"; | ||
/** | ||
* The "User-Agent" header used in all outgoing requests to the Custom Type | ||
* API. It's used by the Custom Type API for tracking purposes. | ||
* Options provided to the client's `fetch()` on all network requests. These | ||
* options will be merged with internally required options. They can also be | ||
* overriden on a per-query basis using the query's `fetchOptions` parameter. | ||
*/ | ||
userAgent?: string; | ||
fetchOptions?: RequestInitLike; | ||
}; | ||
@@ -60,14 +61,17 @@ | ||
export type CustomTypesClientMethodParams = Partial< | ||
Pick< | ||
CustomTypesClientConfig, | ||
"repositoryName" | "endpoint" | "token" | "userAgent" | ||
> | ||
Pick<CustomTypesClientConfig, "repositoryName" | "endpoint" | "token"> | ||
>; | ||
/** | ||
* Parameters for any client method that use `fetch()`. Only a subset of | ||
* `fetch()` parameters are exposed. | ||
* Parameters for client methods that use `fetch()`. | ||
*/ | ||
type FetchParams = { | ||
/** | ||
* Options provided to the client's `fetch()` on all network requests. These | ||
* options will be merged with internally required options. They can also be | ||
* overriden on a per-query basis using the query's `fetchOptions` parameter. | ||
*/ | ||
fetchOptions?: RequestInitLike; | ||
/** | ||
* An `AbortSignal` provided by an `AbortController`. This allows the network | ||
@@ -136,6 +140,7 @@ * request to be cancelled if necessary. | ||
/** | ||
* The "User-Agent" header used in all outgoing requests to the Custom Type | ||
* API. It's used by the Custom Type API for tracking purposes. | ||
* Options provided to the client's `fetch()` on all network requests. These | ||
* options will be merged with internally required options. They can also be | ||
* overriden on a per-query basis using the query's `fetchOptions` parameter. | ||
*/ | ||
userAgent?: string; | ||
fetchOptions?: RequestInitLike; | ||
@@ -149,3 +154,3 @@ /** | ||
this.token = config.token; | ||
this.userAgent = config.userAgent; | ||
this.fetchOptions = config.fetchOptions; | ||
@@ -440,5 +445,7 @@ // TODO: Remove the following `if` statement in v2. | ||
).toString(); | ||
const userAgent = params.userAgent || this.userAgent; | ||
const res = await this.fetchFn(url, { | ||
...this.fetchOptions, | ||
...requestInit, | ||
...params.fetchOptions, | ||
headers: { | ||
@@ -448,6 +455,11 @@ "Content-Type": "application/json", | ||
Authorization: `Bearer ${params.token || this.token}`, | ||
...(userAgent && { "User-Agent": userAgent }), | ||
...this.fetchOptions?.headers, | ||
...requestInit.headers, | ||
...params.fetchOptions?.headers, | ||
}, | ||
signal: params.signal, | ||
...requestInit, | ||
signal: | ||
params.fetchOptions?.signal || | ||
params.signal || | ||
requestInit.signal || | ||
this.fetchOptions?.signal, | ||
}); | ||
@@ -454,0 +466,0 @@ |
@@ -25,5 +25,10 @@ /** | ||
/** | ||
* The minimum required properties from RequestInit. | ||
* A subset of RequestInit properties to configure a `fetch()` request. | ||
*/ | ||
export interface RequestInitLike { | ||
// Only options relevant to the client are included. Extending from the full | ||
// RequestInit would cause issues, such as accepting Header objects. | ||
// | ||
// An interface is used to allow other libraries to augment the type with | ||
// environment-specific types. | ||
export interface RequestInitLike extends Partial<Pick<RequestInit, "cache">> { | ||
// Explicit method names are given for compatibility with `fetch-h2`. | ||
@@ -33,4 +38,19 @@ // Most fetch implementation use `method?: string`, which is compatible | ||
method?: "GET" | "POST" | "DELETE"; | ||
body?: string; | ||
/** | ||
* An object literal to set the `fetch()` request's headers. | ||
*/ | ||
headers?: Record<string, string>; | ||
/** | ||
* An AbortSignal to set the `fetch()` request's signal. | ||
* | ||
* See: | ||
* [https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) | ||
*/ | ||
// NOTE: `AbortSignalLike` is `any`! It is left as `AbortSignalLike` | ||
// for backwards compatibility (the type is exported) and to signal to | ||
// other readers that this should be an AbortSignal-like object. | ||
signal?: AbortSignalLike; | ||
@@ -37,0 +57,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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
117649
1386
1