-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.ts
93 lines (71 loc) · 2.42 KB
/
types.ts
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
import { z } from "./deps.ts";
import type { ZodType } from "./deps.ts";
export const ErrorResponse = z.object({
errors: z.array(z.string()),
});
export type ErrorResponse = z.infer<typeof ErrorResponse>;
export const TokenType = z.enum([
"batch",
"service",
]);
export type TokenType = z.infer<typeof TokenType>;
export const AuthData = z.object({
client_token: z.string(),
accessor: z.string(),
policies: z.array(z.string()),
token_policies: z.array(z.string()),
lease_duration: z.number(),
renewable: z.boolean(),
token_type: TokenType,
});
export type AuthData = z.infer<typeof AuthData>;
export const LoginResponse = z.object({
auth: AuthData,
});
export type LoginResponse = z.infer<typeof AuthData>;
export const TokenLookupResponse = createGenericResponse(z.object({
accessor: z.string().optional(),
renewable: z.boolean(),
ttl: z.number(),
type: TokenType,
}));
export type TokenLookupResponse = z.infer<typeof TokenLookupResponse>;
export function createGenericResponse<T extends ZodType>(response: T) {
return z.object({
data: response,
});
}
export type GenericResponse<T extends ZodType> = z.infer<ReturnType<typeof createGenericResponse<T>>>;
export const WrapInfo = z.object({
token: z.string(),
accessor: z.string(),
ttl: z.number(),
creation_time: z.string(),
creation_path: z.string(),
wrapped_accessor: z.string(),
});
export type WrapInfo = z.infer<typeof WrapInfo>;
export const WrapResponse = z.object({
wrap_info: WrapInfo,
});
export type WrapResponse = z.infer<typeof WrapResponse>;
export function createReadResponse<T extends ZodType>(response: T) {
return z.object({
request_id: z.string(),
lease_id: z.string(),
renewable: z.boolean(),
lease_duration: z.number(),
data: response,
wrap_info: z.nullable(WrapInfo),
warnings: z.array(z.string()).nullish(),
});
}
export type ReadResponse<T extends ZodType> = z.infer<ReturnType<typeof createReadResponse<T>>>;
export function createKVReadResponse<T extends ZodType>(response: T) {
return createReadResponse(z.object({
data: response,
}));
}
export type KVReadResponse<T extends ZodType> = z.infer<ReturnType<typeof createKVReadResponse<T>>>;
export const KVListResponse = createReadResponse(z.object({ keys: z.array(z.string()) }));
export type KVListResponse = z.infer<typeof KVListResponse>;