Gathering detailed insights and metrics for zetch
Gathering detailed insights and metrics for zetch
Gathering detailed insights and metrics for zetch
Gathering detailed insights and metrics for zetch
npm install zetch
Typescript
Module System
Node Version
NPM Version
68.1
Supply Chain
96.6
Quality
75.9
Maintenance
100
Vulnerability
100
License
Cumulative downloads
Total Downloads
Last Day
0%
1
Compared to previous day
Last Week
-79.2%
5
Compared to previous week
Last Month
-11.4%
452
Compared to previous month
Last Year
257%
1,246
Compared to previous year
1
22
HTTP Client with static type inference from your Zod Schemas
Zetch is a HTTP Client that allows you to get static type inference from your existing Zod schemas.
Zetch is designed to be as simple, light-weight and un-opinionated as possible. The goal is that this will eliminate the need for you to manually pass in types to your API calls. Instead, you can just pass in your Zod schema associated with the API call and get immediate static type inference.
Some key features of Zetch are:
1npm install zetch # npm 2yarn add zetch # yarn 3bun add zetch # bun 4pnpm add zetch # pnpm
Making a GET request
1// Create a schema for a Post 2const postSchema = z.object({ 3 title: z.string(), 4 body: z.string(), 5 id: z.number(), 6 userId: z.number(), 7}); 8 9// Make a request to get Posts with your schema provided to get static type inference 10await zetch.get('https://jsonplaceholder.typicode.com/posts', { 11 validationSchema: z.array(postSchema), 12});
Making a request with a body
1// Make a POST request with your body 2await zetch.post('/posts', { 3 body: { 4 title: 'foo', 5 body: 'bar', 6 userId: 1, 7 id: 1, 8 }, 9}); 10 11// Make a POST request with FormData 12const formData = new FormData(); 13formData.append('title', 'foo'); 14formData.append('body', 'bar'); 15formData.append('userId', '1'); 16formData.append('id', '1'); 17 18await zetch.post('/posts', { 19 body: formData, 20});
Creating an API client and making a request
1// Create a Zetch Client with a Base Url 2const zetchClient = zetch.create({ 3 baseUrl: 'https://jsonplaceholder.typicode.com', 4}); 5 6// Create a schema for a Post 7const postSchema = z.object({ 8 title: z.string(), 9 body: z.string(), 10 id: z.number(), 11 userId: z.number(), 12}); 13 14// Make a request to get Posts with your schema provided to get static type inference 15const result = await zetchClient.get('/posts', { 16 validationSchema: z.array(postSchema), 17});
AuthConfig is used to configure the Authorization header for your requests and add in refreshing your token prior to a retried request.
1interface AuthConfig { 2 // The function you'd like called to refresh the token 3 refreshToken?: () => Promise<string>; 4 // The token scheme you'd like to use (Basic, Bearer, JWTBearer) 5 tokenScheme: TokenScheme; 6 // Function to return the token you'd like to use 7 token: () => string; 8}
Retries Config allows you to configure the number of retries and status codes for retries.
1interface RetriesConfig { 2 // Status codes you'd like to retry on 3 retryStatuses: number[]; 4 // The max number of retries you'd like to allow 5 numberOfRetries?: number; 6}
Base Zetch Config that's utilized for both requests and creating an API client
1interface BaseZetchConfig { 2 headers?: Headers; 3 4 // Configuration for authentication 5 authConfig?: AuthConfig; 6 7 // Configuration for retries 8 retriesConfig?: RetriesConfig; 9 10 // The function you'd like to use to log API errors to your error service of choice 11 onApiError?: (error: ZetchError) => void; 12 13 // The function you'd like to use to log API Validation errors to your error service of choice 14 onApiValidationError?: (error: ZodError) => void; 15}
Configs for a Zetch Request
1// Config used for all Zetch Requests except for GET requests 2interface ZetchRequestConfig<ValidationSchema extends ZodFirstPartySchemaTypes> 3 extends BaseZetchConfig { 4 // The validation schema you'd like to use for the response 5 validationSchema?: ValidationSchema; 6 7 // The request body you'd like to send with the request 8 body?: FormData | unknown[] | { [key: string]: unknown }; 9 10 // The abort controller you'd like to use for this request, in the event you would like to cancel the request 11 abortController?: AbortController; 12} 13 14// Config used for GET requests 15type ZetchGetRequestConfig< 16 ValidationSchema extends ZodFirstPartySchemaTypes 17> = Omit<ZetchRequestConfig<ValidationSchema>, 'body'>;
Config for your Zetch API Client from using zetch.create
1interface ZetchClientConfig extends BaseZetchConfig { 2 // The base url for your client 3 baseUrl: string; 4}
No vulnerabilities found.