Comparing version 3.1.0 to 3.1.1
@@ -11,3 +11,3 @@ import { UseQueryOptions } from "react-query"; | ||
* @param apiName - the name of the api used in Zodios Provider | ||
* @param url - the url to call | ||
* @param path - the path to api endpoint | ||
* @param config - the config to setup axios options and parameters | ||
@@ -18,10 +18,11 @@ * @param queryOptions - the query options to setup react-query options | ||
* @example ```typescript | ||
* type Api = ApiOf<typeof myZodiosClient>; | ||
* function useJsonPlaceholder<Path extends Paths<Api, "get">>( | ||
* url: Path, | ||
* path: Path, | ||
* config?: ZodiosRequestOptions<Api, "get", Path> | ||
* ) { | ||
* return useZodios("https://jsonplaceholder.typicode.com", url, config); | ||
* return useZodios("https://jsonplaceholder.typicode.com", path, config); | ||
* } | ||
* ``` | ||
*/ | ||
export declare function useZodios<URL extends string, Api extends ZodiosEnpointDescriptions, Path extends Paths<Api, "get">>(baseUrl: URL, url: Path, config?: ZodiosRequestOptions<Api, "get", Path>, queryOptions?: Omit<UseQueryOptions, "queryKey" | "queryFn">): import("react-query").UseQueryResult<Awaited<import("../zodios.types").Response<Api, "get", Path>>, unknown>; | ||
export declare function useZodios<URL extends string, Api extends ZodiosEnpointDescriptions, Path extends Paths<Api, "get">>(baseUrl: URL, path: Path, config?: ZodiosRequestOptions<Api, "get", Path>, queryOptions?: Omit<UseQueryOptions, "queryKey" | "queryFn">): import("react-query").UseQueryResult<Awaited<import("../zodios.types").Response<Api, "get", Path>>, unknown>; |
@@ -7,2 +7,3 @@ "use strict"; | ||
const provider_1 = require("./provider"); | ||
const utils_1 = require("../utils"); | ||
/** | ||
@@ -15,3 +16,3 @@ * React hook to call an api "get" endpoint. | ||
* @param apiName - the name of the api used in Zodios Provider | ||
* @param url - the url to call | ||
* @param path - the path to api endpoint | ||
* @param config - the config to setup axios options and parameters | ||
@@ -22,15 +23,21 @@ * @param queryOptions - the query options to setup react-query options | ||
* @example ```typescript | ||
* type Api = ApiOf<typeof myZodiosClient>; | ||
* function useJsonPlaceholder<Path extends Paths<Api, "get">>( | ||
* url: Path, | ||
* path: Path, | ||
* config?: ZodiosRequestOptions<Api, "get", Path> | ||
* ) { | ||
* return useZodios("https://jsonplaceholder.typicode.com", url, config); | ||
* return useZodios("https://jsonplaceholder.typicode.com", path, config); | ||
* } | ||
* ``` | ||
*/ | ||
function useZodios(baseUrl, url, config, queryOptions) { | ||
function useZodios(baseUrl, path, config, queryOptions) { | ||
const zodios = (0, react_1.useContext)(provider_1.ZodiosContext)[baseUrl]; | ||
const query = () => zodios.get(url, config); | ||
return (0, react_query_1.useQuery)([baseUrl, url, config], query, queryOptions); | ||
const params = (0, utils_1.pick)(config, [ | ||
"params", | ||
"queries", | ||
]); | ||
const keys = [baseUrl, path, params]; | ||
const query = () => zodios.get(path, config); | ||
return (0, react_query_1.useQuery)(keys, query, queryOptions); | ||
} | ||
exports.useZodios = useZodios; |
@@ -7,2 +7,9 @@ /** | ||
*/ | ||
export declare function omit<T, K extends keyof T>(obj: T, keys: K[]): Omit<T, K>; | ||
export declare function omit<T, K extends keyof T>(obj: T | undefined, keys: K[]): Omit<T, K>; | ||
/** | ||
* pick properties from an object | ||
* @param obj - the object to pick properties from | ||
* @param keys - the keys to pick | ||
* @returns the object with the picked properties | ||
*/ | ||
export declare function pick<T, K extends keyof T>(obj: T | undefined, keys: K[]): Pick<T, K>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.omit = void 0; | ||
exports.pick = exports.omit = void 0; | ||
/** | ||
@@ -18,1 +18,17 @@ * omit properties from an object | ||
exports.omit = omit; | ||
/** | ||
* pick properties from an object | ||
* @param obj - the object to pick properties from | ||
* @param keys - the keys to pick | ||
* @returns the object with the picked properties | ||
*/ | ||
function pick(obj, keys) { | ||
const ret = {}; | ||
if (obj) { | ||
for (const key of keys) { | ||
ret[key] = obj[key]; | ||
} | ||
} | ||
return ret; | ||
} | ||
exports.pick = pick; |
@@ -54,3 +54,3 @@ import { AxiosInstance } from "axios"; | ||
* @param method - the method to use | ||
* @param url - the url to api domain | ||
* @param path - the path to api endpoint | ||
* @param data - the data to send | ||
@@ -60,13 +60,13 @@ * @param config - the config to setup axios options and parameters | ||
*/ | ||
request<M extends Method, Path extends Paths<Api, M>>(method: M, url: Path, data?: Body<Api, M, Path>, config?: ZodiosRequestOptions<Api, M, Path>): Promise<Response<Api, M, Path>>; | ||
request<M extends Method, Path extends Paths<Api, M>>(method: M, path: Path, data?: Body<Api, M, Path>, config?: ZodiosRequestOptions<Api, M, Path>): Promise<Response<Api, M, Path>>; | ||
/** | ||
* make a get request to the api | ||
* @param url - the url to api domain | ||
* @param path - the path to api endpoint | ||
* @param config - the config to setup axios options and parameters | ||
* @returns response validated with zod schema provided in the api description | ||
*/ | ||
get<Path extends Paths<Api, "get">>(url: Path, config?: ZodiosRequestOptions<Api, "get", Path>): Promise<Response<Api, "get", Path>>; | ||
get<Path extends Paths<Api, "get">>(path: Path, config?: ZodiosRequestOptions<Api, "get", Path>): Promise<Response<Api, "get", Path>>; | ||
/** | ||
* make a post request to the api | ||
* @param url - the url to api domain | ||
* @param path - the path to api endpoint | ||
* @param data - the data to send | ||
@@ -76,6 +76,6 @@ * @param config - the config to setup axios options and parameters | ||
*/ | ||
post<Path extends Paths<Api, "post">>(url: Path, data?: Body<Api, "post", Path>, config?: ZodiosRequestOptions<Api, "post", Path>): Promise<Response<Api, "post", Path>>; | ||
post<Path extends Paths<Api, "post">>(path: Path, data?: Body<Api, "post", Path>, config?: ZodiosRequestOptions<Api, "post", Path>): Promise<Response<Api, "post", Path>>; | ||
/** | ||
* make a put request to the api | ||
* @param url - the url to api domain | ||
* @param path - the path to api endpoint | ||
* @param data - the data to send | ||
@@ -85,6 +85,6 @@ * @param config - the config to setup axios options and parameters | ||
*/ | ||
put<Path extends Paths<Api, "put">>(url: Path, data?: Body<Api, "put", Path>, config?: ZodiosRequestOptions<Api, "put", Path>): Promise<Response<Api, "put", Path>>; | ||
put<Path extends Paths<Api, "put">>(path: Path, data?: Body<Api, "put", Path>, config?: ZodiosRequestOptions<Api, "put", Path>): Promise<Response<Api, "put", Path>>; | ||
/** | ||
* make a patch request to the api | ||
* @param url - the url to api domain | ||
* @param path - the path to api endpoint | ||
* @param data - the data to send | ||
@@ -94,12 +94,20 @@ * @param config - the config to setup axios options and parameters | ||
*/ | ||
patch<Path extends Paths<Api, "patch">>(url: Path, data?: Body<Api, "patch", Path>, config?: ZodiosRequestOptions<Api, "patch", Path>): Promise<Response<Api, "patch", Path>>; | ||
patch<Path extends Paths<Api, "patch">>(path: Path, data?: Body<Api, "patch", Path>, config?: ZodiosRequestOptions<Api, "patch", Path>): Promise<Response<Api, "patch", Path>>; | ||
/** | ||
* make a delete request to the api | ||
* @param url - the url to api domain | ||
* @param path - the path to api endpoint | ||
* @param config - the config to setup axios options and parameters | ||
* @returns response validated with zod schema provided in the api description | ||
*/ | ||
delete<Path extends Paths<Api, "delete">>(url: Path, config?: ZodiosRequestOptions<Api, "delete", Path>): Promise<Response<Api, "delete", Path>>; | ||
delete<Path extends Paths<Api, "delete">>(path: Path, config?: ZodiosRequestOptions<Api, "delete", Path>): Promise<Response<Api, "delete", Path>>; | ||
} | ||
export declare type ApiOf<Z> = Z extends Zodios<infer Url, infer Api> ? Api : never; | ||
export declare type UrlOf<Z> = Z extends Zodios<infer Url, infer Api> ? Url : never; | ||
/** | ||
* Get the Api description type from zodios | ||
* @param Z - zodios type | ||
*/ | ||
export declare type ApiOf<Z extends Zodios<any, any>> = Z extends Zodios<any, infer Api> ? Api : never; | ||
/** | ||
* Get the Url string type from zodios | ||
* @param Z - zodios type | ||
*/ | ||
export declare type UrlOf<Z extends Zodios<any, any>> = Z extends Zodios<infer Url, any> ? Url : never; |
@@ -78,4 +78,4 @@ "use strict"; | ||
} | ||
findEndpoint(method, url) { | ||
return this.api.find((e) => e.method === method && e.path === url); | ||
findEndpoint(method, path) { | ||
return this.api.find((e) => e.method === method && e.path === path); | ||
} | ||
@@ -90,4 +90,4 @@ validateResponse(endpoint, response) { | ||
} | ||
replacePathParams(url, anyConfig) { | ||
let result = url; | ||
replacePathParams(path, anyConfig) { | ||
let result = path; | ||
const params = anyConfig === null || anyConfig === void 0 ? void 0 : anyConfig.params; | ||
@@ -102,3 +102,3 @@ if (params) { | ||
* @param method - the method to use | ||
* @param url - the url to api domain | ||
* @param path - the path to api endpoint | ||
* @param data - the data to send | ||
@@ -108,8 +108,8 @@ * @param config - the config to setup axios options and parameters | ||
*/ | ||
async request(method, url, data, config) { | ||
async request(method, path, data, config) { | ||
var _a; | ||
const endpoint = this.findEndpoint(method, url); | ||
const endpoint = this.findEndpoint(method, path); | ||
// istanbul ignore next | ||
if (!endpoint) { | ||
throw new Error(`No endpoint found for ${method} ${url}`); | ||
throw new Error(`No endpoint found for ${method} ${path}`); | ||
} | ||
@@ -119,3 +119,3 @@ const requestConfig = { | ||
method, | ||
url: this.replacePathParams(url, config), | ||
url: this.replacePathParams(path, config), | ||
params: (_a = config) === null || _a === void 0 ? void 0 : _a.queries, | ||
@@ -132,12 +132,12 @@ data, | ||
* make a get request to the api | ||
* @param url - the url to api domain | ||
* @param path - the path to api endpoint | ||
* @param config - the config to setup axios options and parameters | ||
* @returns response validated with zod schema provided in the api description | ||
*/ | ||
async get(url, config) { | ||
return this.request("get", url, undefined, config); | ||
async get(path, config) { | ||
return this.request("get", path, undefined, config); | ||
} | ||
/** | ||
* make a post request to the api | ||
* @param url - the url to api domain | ||
* @param path - the path to api endpoint | ||
* @param data - the data to send | ||
@@ -147,8 +147,8 @@ * @param config - the config to setup axios options and parameters | ||
*/ | ||
async post(url, data, config) { | ||
return this.request("post", url, data, config); | ||
async post(path, data, config) { | ||
return this.request("post", path, data, config); | ||
} | ||
/** | ||
* make a put request to the api | ||
* @param url - the url to api domain | ||
* @param path - the path to api endpoint | ||
* @param data - the data to send | ||
@@ -158,8 +158,8 @@ * @param config - the config to setup axios options and parameters | ||
*/ | ||
async put(url, data, config) { | ||
return this.request("put", url, data, config); | ||
async put(path, data, config) { | ||
return this.request("put", path, data, config); | ||
} | ||
/** | ||
* make a patch request to the api | ||
* @param url - the url to api domain | ||
* @param path - the path to api endpoint | ||
* @param data - the data to send | ||
@@ -169,15 +169,15 @@ * @param config - the config to setup axios options and parameters | ||
*/ | ||
async patch(url, data, config) { | ||
return this.request("patch", url, data, config); | ||
async patch(path, data, config) { | ||
return this.request("patch", path, data, config); | ||
} | ||
/** | ||
* make a delete request to the api | ||
* @param url - the url to api domain | ||
* @param path - the path to api endpoint | ||
* @param config - the config to setup axios options and parameters | ||
* @returns response validated with zod schema provided in the api description | ||
*/ | ||
async delete(url, config) { | ||
return this.request("delete", url, undefined, config); | ||
async delete(path, config) { | ||
return this.request("delete", path, undefined, config); | ||
} | ||
} | ||
exports.Zodios = Zodios; |
@@ -28,3 +28,3 @@ import { AxiosInstance, AxiosRequestConfig } from "axios"; | ||
headers?: Record<string, string>; | ||
} & Omit<AxiosRequestConfig, "params" | "headers" | "baseURL" | "data" | "method">; | ||
} & Omit<AxiosRequestConfig, "params" | "headers" | "baseURL" | "data" | "method" | "url">; | ||
export declare type ZodiosRequestOptions<Api, M extends Method, Path extends string> = SetPropsOptionalIfChildrenAreOptional<PickDefined<{ | ||
@@ -31,0 +31,0 @@ params: PathParams<Path>; |
{ | ||
"name": "zodios", | ||
"description": "Typescript API client with autocompletion and zod validations", | ||
"version": "3.1.0", | ||
"version": "3.1.1", | ||
"main": "lib/index.js", | ||
@@ -6,0 +6,0 @@ "typings": "lib/index.d.ts", |
@@ -68,3 +68,3 @@ <h1 align="center">Zodios</h1> | ||
); | ||
// typed auto-complete url auto-complete params | ||
// typed auto-complete path auto-complete params | ||
// ▼ ▼ ▼ | ||
@@ -154,4 +154,4 @@ const user = await apiClient.get("/users/:id", { params: { id: 7 } }); | ||
function useJsonPlaceholder<Path extends Paths<Api, "get">>(url: Path, config?: ZodiosRequestOptions<Api, "get", Path>) { | ||
return useZodios(baseUrl, url, config); | ||
function useJsonPlaceholder<Path extends Paths<Api, "get">>(path: Path, config?: ZodiosRequestOptions<Api, "get", Path>) { | ||
return useZodios(baseUrl, path, config); | ||
} | ||
@@ -158,0 +158,0 @@ |
35353
737