@zayne-labs/callapi 1.11.9 → 1.11.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -59,8 +59,8 @@ const { data } = await callApi("/api/data"); // JSON? Parsed.
59
59
  const { data, error } = await callApi("/api/users");
60
60
 
61
61
  if (error) {
62
- console.log(error.name); // "HTTPError", "ValidationError"
63
- console.log(error.errorData); // Actual API response
62
+ console.log(error.name); // "HTTPError", "ValidationError"
63
+ console.log(error.errorData); // Actual API response
64
64
  }
65
65
  ```
66
66
 
@@ -68,9 +68,9 @@ if (error) {
68
68
 
69
69
  ```js
70
70
  await callApi("/api/data", {
71
- retryAttempts: 3,
72
- retryStrategy: "exponential",
73
- retryStatusCodes: [429, 500, 502, 503],
71
+ retryAttempts: 3,
72
+ retryStrategy: "exponential",
73
+ retryStatusCodes: [429, 500, 502, 503],
74
74
  });
75
75
  ```
76
76
 
@@ -82,32 +82,35 @@ import { createFetchClient } from "@zayne-labs/callapi";
82
82
  import { defineSchema } from "@zayne-labs/callapi/utils";
83
83
 
84
84
  const callMainApi = createFetchClient({
85
- schema: defineSchema({
86
- "/users/:id": {
87
- data: z.object({
88
- id: z.number(),
89
- name: z.string(),
90
- }),
91
- },
92
- }),
85
+ schema: defineSchema({
86
+ "/users/:id": {
87
+ data: z.object({
88
+ id: z.number(),
89
+ name: z.string(),
90
+ }),
91
+ },
92
+ }),
93
93
  });
94
94
 
95
- const user = await callMainApi("/users/123"); // Fully typed + validated
95
+ // Fully typed + validated
96
+ const user = await callMainApi("/users/:id", {
97
+ params: { id: 123 },
98
+ });
96
99
  ```
97
100
 
98
101
  **Hooks** - Intercept at any point.
99
102
 
100
103
  ```js
101
104
  const api = createFetchClient({
102
- onRequest: ({ request }) => {
103
- request.headers.set("Authorization", `Bearer ${token}`);
104
- },
105
- onError: ({ error }) => {
106
- Sentry.captureException(error);
107
- },
108
- onResponseStream: ({ event }) => {
109
- console.log(`Downloaded ${event.progress}%`);
110
- },
105
+ onRequest: ({ request }) => {
106
+ request.headers.set("Authorization", `Bearer ${token}`);
107
+ },
108
+ onError: ({ error }) => {
109
+ Sentry.captureException(error);
110
+ },
111
+ onResponseStream: ({ event }) => {
112
+ console.log(`Downloaded ${event.progress}%`);
113
+ },
111
114
  });
112
115
  ```
113
116
 
@@ -115,38 +118,38 @@ const api = createFetchClient({
115
118
 
116
119
  ```js
117
120
  const metricsPlugin = definePlugin({
118
- id: "metrics",
119
- name: "Metrics Plugin",
120
-
121
- setup: ({ options }) => ({
122
- options: {
123
- ...options,
124
- meta: { startTime: Date.now() },
125
- },
126
- }),
127
-
128
- hooks: {
129
- onSuccess: ({ options }) => {
130
- const duration = Date.now() - options.meta.startTime;
131
-
132
- console.info(`Request took ${duration}ms`);
133
- },
134
- },
135
-
136
- middlewares: {
137
- fetchMiddleware: (ctx) => async (input, init) => {
138
- console.info("→", input);
139
-
140
- const response = await ctx.fetchImpl(input, init);
141
-
142
- console.info("←", response.status);
143
- return response;
144
- },
145
- },
121
+ id: "metrics",
122
+ name: "Metrics Plugin",
123
+
124
+ setup: ({ options }) => ({
125
+ options: {
126
+ ...options,
127
+ meta: { startTime: Date.now() },
128
+ },
129
+ }),
130
+
131
+ hooks: {
132
+ onSuccess: ({ options }) => {
133
+ const duration = Date.now() - options.meta.startTime;
134
+
135
+ console.info(`Request took ${duration}ms`);
136
+ },
137
+ },
138
+
139
+ middlewares: {
140
+ fetchMiddleware: (ctx) => async (input, init) => {
141
+ console.info("→", input);
142
+
143
+ const response = await ctx.fetchImpl(input, init);
144
+
145
+ console.info("←", response.status);
146
+ return response;
147
+ },
148
+ },
146
149
  });
147
150
 
148
151
  const api = createFetchClient({
149
- plugins: [metricsPlugin],
152
+ plugins: [metricsPlugin],
150
153
  });
151
154
  ```
152
155
 
@@ -174,10 +177,10 @@ const { data } = await callApi("/api/users");
174
177
 
175
178
  // Configured
176
179
  const api = createFetchClient({
177
- baseURL: "https://api.example.com",
178
- retryAttempts: 2,
179
- timeout: 10000,
180
- onError: ({ error }) => trackError(error),
180
+ baseURL: "https://api.example.com",
181
+ retryAttempts: 2,
182
+ timeout: 10000,
183
+ onError: ({ error }) => trackError(error),
181
184
  });
182
185
  ```
183
186
 
@@ -185,7 +188,7 @@ const api = createFetchClient({
185
188
 
186
189
  ```html
187
190
  <script type="module">
188
- import { callApi } from "https://esm.run/@zayne-labs/callapi";
191
+ import { callApi } from "https://esm.run/@zayne-labs/callapi";
189
192
  </script>
190
193
  ```
191
194