@prismicio/custom-types-client
Version:
JavaScript client to interact with the Prismic Custom Types API
307 lines (306 loc) • 12.9 kB
TypeScript
import type * as prismic from "@prismicio/client";
import type { AbortSignalLike, FetchLike, RequestInitLike } from "./types";
import { BulkUpdateOperation, BulkUpdateTransaction } from "./bulkUpdate";
/**
* Configuration for creating a `CustomTypesClient`.
*/
export type CustomTypesClientConfig = {
/**
* Name of the Prismic repository.
*/
repositoryName: string;
/**
* The Prismic Custom Types API endpoint for the repository. The standard
* Custom Types API endpoint will be used if no value is provided.
*/
endpoint?: string;
/**
* The secure token for accessing the Prismic Custom Types API. This is
* required to call any Custom Type API methods.
*/
token: string;
/**
* The function used to make network requests to the Prismic Custom Types API.
* In environments where a global `fetch` function does not exist, such as
* Node.js, this function must be provided.
*/
fetch?: FetchLike;
/**
* Options provided to the client's `fetch()` on all network requests. These
* options will be merged with internally required options. They can also be
* overridden on a per-query basis using the query's `fetchOptions`
* parameter.
*/
fetchOptions?: RequestInitLike;
};
/**
* Parameters for `CustomTypesClient` methods. Values provided here will
* override the client's default values, if present.
*/
export type CustomTypesClientMethodParams = Partial<Pick<CustomTypesClientConfig, "repositoryName" | "endpoint" | "token">>;
/**
* 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
* request to be cancelled if necessary.
*
* {@link https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal}
*/
signal?: AbortSignalLike;
};
/**
* Create a client for the Prismic Custom Types API.
*/
export declare const createClient: (config: CustomTypesClientConfig) => CustomTypesClient;
/**
* A client for the Prismic Custom Types API.
*
* @see Custom Types API documentation: {@link https://prismic.io/docs/technologies/custom-types-api}
*/
export declare class CustomTypesClient {
/**
* Name of the Prismic repository.
*/
repositoryName: string;
/**
* The Prismic Custom Types API endpoint for the repository. The standard
* Custom Types API endpoint will be used if no value is provided.
*
* @defaultValue `https://customtypes.prismic.io`
*/
endpoint: string;
/**
* The secure token for accessing the Prismic Custom Types API. This is
* required to call any Custom Type API methods.
*/
token: string;
/**
* The function used to make network requests to the Prismic Custom Types API.
* In environments where a global `fetch` function does not exist, such as
* Node.js, this function must be provided.
*/
fetchFn: FetchLike;
/**
* 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;
/**
* Create a client for the Prismic Custom Types API.
*/
constructor(config: CustomTypesClientConfig);
/**
* Returns all Custom Types models from the Prismic repository.
*
* @typeParam TCustomType - The Custom Type returned from the API.
*
* @param params - Parameters to override the client's default configuration.
*
* @returns All Custom Type models from the Prismic repository.
*
* @throws {@link ForbiddenError} Thrown if the client is unauthorized to make
* requests.
*/
getAllCustomTypes<TCustomType extends prismic.CustomTypeModel>(params?: CustomTypesClientMethodParams & FetchParams): Promise<TCustomType[]>;
/**
* Returns a Custom Type model with a given ID from the Prismic repository.
*
* @typeParam TCustomType - The Custom Type returned from the API.
*
* @param id - ID of the Custom Type.
* @param params - Parameters to override the client's default configuration.
*
* @returns The Custom Type model from the Prismic repository.
*
* @throws {@link ForbiddenError} Thrown if the client is unauthorized to make
* requests.
* @throws {@link NotFoundError} Thrown if a Custom Type with the given ID
* cannot be found.
*/
getCustomTypeByID<TCustomType extends prismic.CustomTypeModel>(id: string, params?: CustomTypesClientMethodParams & FetchParams): Promise<TCustomType>;
/**
* Inserts a Custom Type model to the Prismic repository.
*
* @typeParam TCustomType - The Custom Type to insert.
*
* @param customType - The Custom Type to insert.
* @param params - Parameters to override the client's default configuration.
*
* @returns The inserted Custom Type.
*
* @throws {@link ForbiddenError} Thrown if the client is unauthorized to make
* requests.
* @throws {@link InvalidPayloadError} Thrown if an invalid Custom Type is
* provided.
* @throws {@link ConflictError} Thrown if a Custom Type with the given ID
* already exists.
*/
insertCustomType<TCustomType extends prismic.CustomTypeModel>(customType: TCustomType, params?: CustomTypesClientMethodParams & FetchParams): Promise<TCustomType>;
/**
* Updates a Custom Type model from the Prismic repository.
*
* @typeParam TCustomType - The updated Custom Type.
*
* @param customType - The updated Custom Type.
* @param params - Parameters to override the client's default configuration.
*
* @returns The updated Custom Type.
*
* @throws {@link ForbiddenError} Thrown if the client is unauthorized to make
* requests.
* @throws {@link InvalidPayloadError} Thrown if an invalid Custom Type is
* provided.
* @throws {@link NotFoundError} Thrown if a Custom Type with the given ID
* cannot be found.
*/
updateCustomType<TCustomType extends prismic.CustomTypeModel>(customType: TCustomType, params?: CustomTypesClientMethodParams & FetchParams): Promise<TCustomType>;
/**
* Removes a Custom Type model from the Prismic repository.
*
* @typeParam TCustomTypeID - The ID of the Custom Type.
*
* @param id - The ID of the Custom Type to remove.
* @param params - Parameters to override the client's default configuration.
*
* @returns The ID of the removed Custom Type.
*
* @throws {@link ForbiddenError} Thrown if the client is unauthorized to make
* requests.
*/
removeCustomType<TCustomTypeID extends string>(id: TCustomTypeID, params?: CustomTypesClientMethodParams & FetchParams): Promise<TCustomTypeID>;
/**
* Returns all Shared Slice models from the Prismic repository.
*
* @typeParam TSharedSliceModel - The Shared Slice model returned from the
* API.
*
* @param params - Parameters to override the client's default configuration.
*
* @returns All Shared Slice models from the Prismic repository.
*
* @throws {@link ForbiddenError} Thrown if the client is unauthorized to make
* requests.
*/
getAllSharedSlices<TSharedSliceModel extends prismic.SharedSliceModel>(params?: CustomTypesClientMethodParams & FetchParams): Promise<TSharedSliceModel[]>;
/**
* Returns a Shared Slice model with a given ID from the Prismic repository.
*
* @typeParam TSharedSliceModel - The Shared Slice model returned from the
* API.
*
* @param id - ID of the Shared Slice.
* @param params - Parameters to override the client's default configuration.
*
* @returns The Shared Slice model from the Prismic repository.
*
* @throws {@link ForbiddenError} Thrown if the client is unauthorized to make
* requests.
* @throws {@link NotFoundError} Thrown if a Shared Slice with the given ID
* cannot be found.
*/
getSharedSliceByID<TSharedSliceModel extends prismic.SharedSliceModel>(id: string, params?: CustomTypesClientMethodParams & FetchParams): Promise<TSharedSliceModel>;
/**
* Inserts a Shared Slice model to the Prismic repository.
*
* @typeParam TSharedSliceModel - The Shared Slice model to insert.
*
* @param slice - The Shared Slice model to insert.
* @param params - Parameters to override the client's default configuration.
*
* @returns The inserted Shared Slice model.
*
* @throws {@link ForbiddenError} Thrown if the client is unauthorized to make
* requests.
* @throws {@link InvalidPayloadError} Thrown if an invalid Shared Slice model
* is provided.
* @throws {@link ConflictError} Thrown if a Shared Slice with the given ID
* already exists.
*/
insertSharedSlice<TSharedSliceModel extends prismic.SharedSliceModel>(slice: TSharedSliceModel, params?: CustomTypesClientMethodParams & FetchParams): Promise<TSharedSliceModel>;
/**
* Updates a Shared Slice model from the Prismic repository.
*
* @typeParam TSharedSliceModel - The updated Shared Slice model.
*
* @param slice - The updated Shared Slice model.
* @param params - Parameters to override the client's default configuration.
*
* @returns The updated Shared Slice model.
*
* @throws {@link ForbiddenError} Thrown if the client is unauthorized to make
* requests.
* @throws {@link InvalidPayloadError} Thrown if an invalid Shared Slice model
* is provided.
* @throws {@link NotFoundError} Thrown if a Shared Slice with the given ID
* cannot be found.
*/
updateSharedSlice<TSharedSliceModel extends prismic.SharedSliceModel>(slice: TSharedSliceModel, params?: CustomTypesClientMethodParams & FetchParams): Promise<TSharedSliceModel>;
/**
* Removes a Shared Slice model from the Prismic repository.
*
* @typeParam TSharedSliceID - The ID of the Shared Slice.
*
* @param id - The ID of the Shared Slice to remove.
* @param params - Parameters to override the client's default configuration.
*
* @returns The ID of the removed Shared Slice.
*
* @throws {@link ForbiddenError} Thrown if the client is unauthorized to make
* requests.
*/
removeSharedSlice<TSharedSliceID extends string>(id: TSharedSliceID, params?: CustomTypesClientMethodParams & FetchParams): Promise<TSharedSliceID>;
/**
* Performs multiple insert, update, and/or delete operations in a single
* transaction.
*
* @example
*
* ```ts
* const bulkUpdateTransaction = createBulkUpdateTransaction();
* bulkUpdateTransaction.insertCustomType(myCustomType);
* bulkUpdateTransaction.deleteSlice(mySlice);
*
* await client.bulkUpdate(bulkUpdateTransaction);
* ```
*
* @param operations - A `BulkUpdateTransaction` containing all operations or
* an array of objects describing an operation.
* @param params - Parameters that determine how the method behaves and for
* overriding the client's default configuration.
*
* @returns An array of objects describing the operations.
*/
bulkUpdate(operations: BulkUpdateTransaction | BulkUpdateOperation[], params?: CustomTypesClientMethodParams & FetchParams): Promise<BulkUpdateOperation[]>;
/**
* Performs a network request using the configured `fetch` function. It
* assumes all successful responses will have a JSON content type. It also
* normalizes unsuccessful network requests.
*
* @typeParam T - The JSON response.
*
* @param path - URL to the resource to fetch.
* @param params - Parameters to override the client's default configuration.
* @param requestInit - `RequestInit` overrides for the `fetch` request.
*
* @returns The response from the network request, if any.
*
* @throws {@link ForbiddenError} Thrown if the client is unauthorized to make
* requests.
* @throws {@link InvalidPayloadError} Thrown if the given body is invalid.
* @throws {@link ConflictError} Thrown if an entity with the given ID already
* exists.
* @throws {@link NotFoundError} Thrown if the requested entity could not be
* found.
*/
private fetch;
}
export {};