UNPKG

payload-rest-client

Version:

A typesafe rest api client for the payload cms.

86 lines 3.49 kB
"use strict"; // import { fetchFactory } from "./fetch"; // import { FetchOptions } from "./types"; // type Params = Record<string, string>; // type Query = Record<string, any>; // type CustomEndpoint<R> = // () => Promise<R>; // type CustomEndpointWithParams<P extends Params, R> = // (params: { params: P }) => Promise<R>; // type CustomEndpointWithQuery<Q extends Query, R> = // (params: { query: Q }) => Promise<R>; // type CustomEndpointWithParamsAndQuery<P extends Params, Q extends Query, R> = // (params: { params: P; query: Q }) => Promise<R>; // type CEP<P extends Record<string, string>, Q extends Record<string, any>, R> = // | CustomEndpoint<R> // | CustomEndpointWithParams<P, R> // | CustomEndpointWithQuery<Q, R> // | CustomEndpointWithParamsAndQuery<P, Q, R> // type CustomEndpoints = Record<string, CEP<any, any, any>>; // const createCustomEndpointProxy = (options: FetchOptions, urls: any) => { // const fetchFn = fetchFactory(options); // return new Proxy( // {}, // { // get: (_, method: string) => { // return (params: { params?: Params, query?: Query } | void) => { // const url: string = params?.params ? urls[method](params.params) : urls[method](); // // console.log(url) // // Promise.resolve() // return fetchFn({ // type: "custom", // method: "GET", // path: url, // qs: null, // }); // }; // } // } // ); // }; // const create = <C extends CustomEndpoints>(endpoints: C) => { // return createCustomEndpointProxy({ // apiUrl: "http://localhost:3000/api", // cache: "no-store", // debug: true, // customFetchFn: (input, init) => { // console.log("custom fetch", input, init); // return fetch(input, init); // }, // }, endpoints as any) as C; // }; // const CE = { // create: <R>(url: () => string) => { // return url as any as CustomEndpoint<R>; // }, // createWithParams: <P extends Record<string, string>, R>(url: (params: P) => string) => { // return url as any as CustomEndpointWithParams<P, R>; // }, // createWithQuery: <Q extends Record<string, any>, R>(url: () => string) => { // return url as any as CustomEndpointWithQuery<Q, R>; // }, // createWithParamsAndQuery: <P extends Record<string, string>, Q extends Record<string, any>, R>(url: (params: P) => string) => { // return url as any as CustomEndpointWithParamsAndQuery<P, Q, R>; // }, // } // let xx = create({ // getSomething: CE.create<string>("GET", () => `getSomething`), // getSomethingById: CE.createWithParams<{ id: string }, string>(p => `getSomethingById/${p.id}`), // }) // xx.getSomething() // xx.getSomethingById({ params: { id: "1" } }) // type GetMandatoryKeys<T> = { // [P in keyof T]: T[P] extends undefined ? never : P; // }[keyof T]; // type GetMandatoryProps<T> = Pick<T, GetMandatoryKeys<T>>; // type X<R, P = undefined, Q = undefined, B = undefined> = (params: GetMandatoryProps<{ // params: P, // query: Q, // body: B, // }>) => R // const x = {} as X<string, { id: string }> // const y = {} as X<string> // x({ params: { id: "1" } }) // // x({ }) // y({}) //# sourceMappingURL=test.js.map