-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathtoomanyrequests.ts
More file actions
192 lines (173 loc) · 4.59 KB
/
toomanyrequests.ts
File metadata and controls
192 lines (173 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
*/
import * as z from "zod/v3";
import { safeParse } from "../lib/schemas.js";
import { ClosedEnum } from "../types/enums.js";
import { Result as SafeParseResult } from "../types/fp.js";
import * as types from "../types/primitives.js";
import { SDKValidationError } from "./sdkvalidationerror.js";
import { VercelError } from "./vercelerror.js";
export const TooManyRequestsCode = {
TooManyRequests: "too_many_requests",
} as const;
export type TooManyRequestsCode = ClosedEnum<typeof TooManyRequestsCode>;
export type RetryAfter = {
value: number;
str: string;
};
export type Limit = {
total: number;
remaining: number;
reset: number;
};
export type TooManyRequestsData = {
status: number;
code: TooManyRequestsCode;
message: string;
retryAfter: RetryAfter;
limit: Limit;
};
export class TooManyRequests extends VercelError {
status: number;
code: TooManyRequestsCode;
retryAfter: RetryAfter;
limit: Limit;
/** The original data that was passed to this error instance. */
data$: TooManyRequestsData;
constructor(
err: TooManyRequestsData,
httpMeta: { response: Response; request: Request; body: string },
) {
const message = err.message || `API error occurred: ${JSON.stringify(err)}`;
super(message, httpMeta);
this.data$ = err;
this.status = err.status;
this.code = err.code;
this.retryAfter = err.retryAfter;
this.limit = err.limit;
this.name = "TooManyRequests";
}
}
/** @internal */
export const TooManyRequestsCode$inboundSchema: z.ZodNativeEnum<
typeof TooManyRequestsCode
> = z.nativeEnum(TooManyRequestsCode);
/** @internal */
export const TooManyRequestsCode$outboundSchema: z.ZodNativeEnum<
typeof TooManyRequestsCode
> = TooManyRequestsCode$inboundSchema;
/** @internal */
export const RetryAfter$inboundSchema: z.ZodType<
RetryAfter,
z.ZodTypeDef,
unknown
> = z.object({
value: types.number(),
str: types.string(),
});
/** @internal */
export type RetryAfter$Outbound = {
value: number;
str: string;
};
/** @internal */
export const RetryAfter$outboundSchema: z.ZodType<
RetryAfter$Outbound,
z.ZodTypeDef,
RetryAfter
> = z.object({
value: z.number(),
str: z.string(),
});
export function retryAfterToJSON(retryAfter: RetryAfter): string {
return JSON.stringify(RetryAfter$outboundSchema.parse(retryAfter));
}
export function retryAfterFromJSON(
jsonString: string,
): SafeParseResult<RetryAfter, SDKValidationError> {
return safeParse(
jsonString,
(x) => RetryAfter$inboundSchema.parse(JSON.parse(x)),
`Failed to parse 'RetryAfter' from JSON`,
);
}
/** @internal */
export const Limit$inboundSchema: z.ZodType<Limit, z.ZodTypeDef, unknown> = z
.object({
total: types.number(),
remaining: types.number(),
reset: types.number(),
});
/** @internal */
export type Limit$Outbound = {
total: number;
remaining: number;
reset: number;
};
/** @internal */
export const Limit$outboundSchema: z.ZodType<
Limit$Outbound,
z.ZodTypeDef,
Limit
> = z.object({
total: z.number(),
remaining: z.number(),
reset: z.number(),
});
export function limitToJSON(limit: Limit): string {
return JSON.stringify(Limit$outboundSchema.parse(limit));
}
export function limitFromJSON(
jsonString: string,
): SafeParseResult<Limit, SDKValidationError> {
return safeParse(
jsonString,
(x) => Limit$inboundSchema.parse(JSON.parse(x)),
`Failed to parse 'Limit' from JSON`,
);
}
/** @internal */
export const TooManyRequests$inboundSchema: z.ZodType<
TooManyRequests,
z.ZodTypeDef,
unknown
> = z.object({
status: types.number(),
code: TooManyRequestsCode$inboundSchema,
message: types.string(),
retryAfter: z.lazy(() => RetryAfter$inboundSchema),
limit: z.lazy(() => Limit$inboundSchema),
request$: z.instanceof(Request),
response$: z.instanceof(Response),
body$: z.string(),
})
.transform((v) => {
return new TooManyRequests(v, {
request: v.request$,
response: v.response$,
body: v.body$,
});
});
/** @internal */
export type TooManyRequests$Outbound = {
status: number;
code: string;
message: string;
retryAfter: RetryAfter$Outbound;
limit: Limit$Outbound;
};
/** @internal */
export const TooManyRequests$outboundSchema: z.ZodType<
TooManyRequests$Outbound,
z.ZodTypeDef,
TooManyRequests
> = z.instanceof(TooManyRequests)
.transform(v => v.data$)
.pipe(z.object({
status: z.number(),
code: TooManyRequestsCode$outboundSchema,
message: z.string(),
retryAfter: z.lazy(() => RetryAfter$outboundSchema),
limit: z.lazy(() => Limit$outboundSchema),
}));