@darco2903/auth-api 2.0.4-beta.0

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/dist/client.js ADDED
@@ -0,0 +1,32 @@
1
+ import { Second } from "@darco2903/secondthought";
2
+ export * from "./common.js";
3
+ import { initClient } from "@ts-rest/core";
4
+ import contract from "./contract/index.js";
5
+ import { UserRole } from "./roles.js";
6
+ export function createClient(origin) {
7
+ return initClient(contract, {
8
+ baseUrl: origin,
9
+ credentials: "include",
10
+ });
11
+ }
12
+ export function getRoleName(level) {
13
+ return UserRole[level] || "Unknown";
14
+ }
15
+ export function getRoleLevel(name) {
16
+ return UserRole[name] || -1;
17
+ }
18
+ export function accessTokenExpiresAt() {
19
+ if (document) {
20
+ const expiresAtCookie = document.cookie
21
+ .split("; ")
22
+ .find((row) => row.startsWith("expiresAt="))
23
+ ?.split("=")[1];
24
+ if (expiresAtCookie) {
25
+ const expiresAt = Number(expiresAtCookie);
26
+ if (!isNaN(expiresAt)) {
27
+ return new Second(expiresAt).toDate();
28
+ }
29
+ }
30
+ }
31
+ return null;
32
+ }
@@ -0,0 +1,4 @@
1
+ export { default as contract } from "./contract/index.js";
2
+ export * from "./types/index.js";
3
+ export * from "./consts.js";
4
+ export { UserRole } from "./roles.js";
package/dist/common.js ADDED
@@ -0,0 +1,4 @@
1
+ export { default as contract } from "./contract/index.js";
2
+ export * from "./types/index.js";
3
+ export * from "./consts.js";
4
+ export { UserRole } from "./roles.js";
@@ -0,0 +1,10 @@
1
+ import type { Algorithm } from "jsonwebtoken";
2
+ export declare const API_VERSION = "v2";
3
+ export declare const API_PATH_PREFIX = "/api/v2";
4
+ export declare const JWT_ALGORITHM: Algorithm;
5
+ export declare const NAME_MIN_LENGTH = 3;
6
+ export declare const NAME_MAX_LENGTH = 32;
7
+ export declare const EMAIL_MAX_LENGTH = 255;
8
+ export declare const PASSWORD_MIN_LENGTH = 8;
9
+ export declare const PASSWORD_MAX_LENGTH = 72;
10
+ export declare const USER_PUBLIC_ID_LENGTH = 8;
package/dist/consts.js ADDED
@@ -0,0 +1,9 @@
1
+ export const API_VERSION = "v2";
2
+ export const API_PATH_PREFIX = `/api/${API_VERSION}`;
3
+ export const JWT_ALGORITHM = "ES256";
4
+ export const NAME_MIN_LENGTH = 3;
5
+ export const NAME_MAX_LENGTH = 32;
6
+ export const EMAIL_MAX_LENGTH = 255;
7
+ export const PASSWORD_MIN_LENGTH = 8;
8
+ export const PASSWORD_MAX_LENGTH = 72; // bcrypt truncates passwords to 72 characters
9
+ export const USER_PUBLIC_ID_LENGTH = 8;
@@ -0,0 +1,356 @@
1
+ import z from "zod";
2
+ declare const _default: {
3
+ token: {
4
+ body: z.ZodObject<{
5
+ type: z.ZodEnum<["avatar"]>;
6
+ }, "strip", z.ZodTypeAny, {
7
+ type: "avatar";
8
+ }, {
9
+ type: "avatar";
10
+ }>;
11
+ method: "POST";
12
+ path: "/assets/token";
13
+ headers: z.ZodObject<{
14
+ authorization: z.ZodOptional<z.ZodString>;
15
+ }, "strip", z.ZodTypeAny, {
16
+ authorization?: string | undefined;
17
+ }, {
18
+ authorization?: string | undefined;
19
+ }>;
20
+ responses: {
21
+ 200: z.ZodObject<{
22
+ cdnToken: z.ZodString;
23
+ }, "strip", z.ZodTypeAny, {
24
+ cdnToken: string;
25
+ }, {
26
+ cdnToken: string;
27
+ }>;
28
+ 400: z.ZodObject<{
29
+ name: z.ZodLiteral<"ZodError">;
30
+ issues: z.ZodArray<z.ZodObject<{
31
+ path: z.ZodArray<z.ZodUnion<[z.ZodString, z.ZodNumber]>, "many">;
32
+ message: z.ZodOptional<z.ZodString>;
33
+ code: z.ZodNativeEnum<{
34
+ invalid_type: "invalid_type";
35
+ invalid_literal: "invalid_literal";
36
+ custom: "custom";
37
+ invalid_union: "invalid_union";
38
+ invalid_union_discriminator: "invalid_union_discriminator";
39
+ invalid_enum_value: "invalid_enum_value";
40
+ unrecognized_keys: "unrecognized_keys";
41
+ invalid_arguments: "invalid_arguments";
42
+ invalid_return_type: "invalid_return_type";
43
+ invalid_date: "invalid_date";
44
+ invalid_string: "invalid_string";
45
+ too_small: "too_small";
46
+ too_big: "too_big";
47
+ invalid_intersection_types: "invalid_intersection_types";
48
+ not_multiple_of: "not_multiple_of";
49
+ not_finite: "not_finite";
50
+ }>;
51
+ }, "strip", z.ZodAny, z.objectOutputType<{
52
+ path: z.ZodArray<z.ZodUnion<[z.ZodString, z.ZodNumber]>, "many">;
53
+ message: z.ZodOptional<z.ZodString>;
54
+ code: z.ZodNativeEnum<{
55
+ invalid_type: "invalid_type";
56
+ invalid_literal: "invalid_literal";
57
+ custom: "custom";
58
+ invalid_union: "invalid_union";
59
+ invalid_union_discriminator: "invalid_union_discriminator";
60
+ invalid_enum_value: "invalid_enum_value";
61
+ unrecognized_keys: "unrecognized_keys";
62
+ invalid_arguments: "invalid_arguments";
63
+ invalid_return_type: "invalid_return_type";
64
+ invalid_date: "invalid_date";
65
+ invalid_string: "invalid_string";
66
+ too_small: "too_small";
67
+ too_big: "too_big";
68
+ invalid_intersection_types: "invalid_intersection_types";
69
+ not_multiple_of: "not_multiple_of";
70
+ not_finite: "not_finite";
71
+ }>;
72
+ }, z.ZodAny, "strip">, z.objectInputType<{
73
+ path: z.ZodArray<z.ZodUnion<[z.ZodString, z.ZodNumber]>, "many">;
74
+ message: z.ZodOptional<z.ZodString>;
75
+ code: z.ZodNativeEnum<{
76
+ invalid_type: "invalid_type";
77
+ invalid_literal: "invalid_literal";
78
+ custom: "custom";
79
+ invalid_union: "invalid_union";
80
+ invalid_union_discriminator: "invalid_union_discriminator";
81
+ invalid_enum_value: "invalid_enum_value";
82
+ unrecognized_keys: "unrecognized_keys";
83
+ invalid_arguments: "invalid_arguments";
84
+ invalid_return_type: "invalid_return_type";
85
+ invalid_date: "invalid_date";
86
+ invalid_string: "invalid_string";
87
+ too_small: "too_small";
88
+ too_big: "too_big";
89
+ invalid_intersection_types: "invalid_intersection_types";
90
+ not_multiple_of: "not_multiple_of";
91
+ not_finite: "not_finite";
92
+ }>;
93
+ }, z.ZodAny, "strip">>, "many">;
94
+ }, "strip", z.ZodTypeAny, {
95
+ name: "ZodError";
96
+ issues: z.objectOutputType<{
97
+ path: z.ZodArray<z.ZodUnion<[z.ZodString, z.ZodNumber]>, "many">;
98
+ message: z.ZodOptional<z.ZodString>;
99
+ code: z.ZodNativeEnum<{
100
+ invalid_type: "invalid_type";
101
+ invalid_literal: "invalid_literal";
102
+ custom: "custom";
103
+ invalid_union: "invalid_union";
104
+ invalid_union_discriminator: "invalid_union_discriminator";
105
+ invalid_enum_value: "invalid_enum_value";
106
+ unrecognized_keys: "unrecognized_keys";
107
+ invalid_arguments: "invalid_arguments";
108
+ invalid_return_type: "invalid_return_type";
109
+ invalid_date: "invalid_date";
110
+ invalid_string: "invalid_string";
111
+ too_small: "too_small";
112
+ too_big: "too_big";
113
+ invalid_intersection_types: "invalid_intersection_types";
114
+ not_multiple_of: "not_multiple_of";
115
+ not_finite: "not_finite";
116
+ }>;
117
+ }, z.ZodAny, "strip">[];
118
+ }, {
119
+ name: "ZodError";
120
+ issues: z.objectInputType<{
121
+ path: z.ZodArray<z.ZodUnion<[z.ZodString, z.ZodNumber]>, "many">;
122
+ message: z.ZodOptional<z.ZodString>;
123
+ code: z.ZodNativeEnum<{
124
+ invalid_type: "invalid_type";
125
+ invalid_literal: "invalid_literal";
126
+ custom: "custom";
127
+ invalid_union: "invalid_union";
128
+ invalid_union_discriminator: "invalid_union_discriminator";
129
+ invalid_enum_value: "invalid_enum_value";
130
+ unrecognized_keys: "unrecognized_keys";
131
+ invalid_arguments: "invalid_arguments";
132
+ invalid_return_type: "invalid_return_type";
133
+ invalid_date: "invalid_date";
134
+ invalid_string: "invalid_string";
135
+ too_small: "too_small";
136
+ too_big: "too_big";
137
+ invalid_intersection_types: "invalid_intersection_types";
138
+ not_multiple_of: "not_multiple_of";
139
+ not_finite: "not_finite";
140
+ }>;
141
+ }, z.ZodAny, "strip">[];
142
+ }>;
143
+ 401: z.ZodObject<{
144
+ code: z.ZodType<"UNAUTHORIZED", z.ZodTypeDef, "UNAUTHORIZED">;
145
+ error: z.ZodType<"Unauthorized", z.ZodTypeDef, "Unauthorized">;
146
+ name: z.ZodLiteral<"APIError">;
147
+ }, "strip", z.ZodTypeAny, {
148
+ code: "UNAUTHORIZED";
149
+ name: "APIError";
150
+ error: "Unauthorized";
151
+ }, {
152
+ code: "UNAUTHORIZED";
153
+ name: "APIError";
154
+ error: "Unauthorized";
155
+ }>;
156
+ };
157
+ };
158
+ update: {
159
+ body: typeof import("@ts-rest/core").ContractNoBody;
160
+ method: "POST";
161
+ path: "/assets";
162
+ headers: z.ZodObject<{
163
+ authorization: z.ZodString;
164
+ }, "strip", z.ZodTypeAny, {
165
+ authorization: string;
166
+ }, {
167
+ authorization: string;
168
+ }>;
169
+ responses: {
170
+ 200: typeof import("@ts-rest/core").ContractNoBody;
171
+ 400: z.ZodObject<{
172
+ name: z.ZodLiteral<"ZodError">;
173
+ issues: z.ZodArray<z.ZodObject<{
174
+ path: z.ZodArray<z.ZodUnion<[z.ZodString, z.ZodNumber]>, "many">;
175
+ message: z.ZodOptional<z.ZodString>;
176
+ code: z.ZodNativeEnum<{
177
+ invalid_type: "invalid_type";
178
+ invalid_literal: "invalid_literal";
179
+ custom: "custom";
180
+ invalid_union: "invalid_union";
181
+ invalid_union_discriminator: "invalid_union_discriminator";
182
+ invalid_enum_value: "invalid_enum_value";
183
+ unrecognized_keys: "unrecognized_keys";
184
+ invalid_arguments: "invalid_arguments";
185
+ invalid_return_type: "invalid_return_type";
186
+ invalid_date: "invalid_date";
187
+ invalid_string: "invalid_string";
188
+ too_small: "too_small";
189
+ too_big: "too_big";
190
+ invalid_intersection_types: "invalid_intersection_types";
191
+ not_multiple_of: "not_multiple_of";
192
+ not_finite: "not_finite";
193
+ }>;
194
+ }, "strip", z.ZodAny, z.objectOutputType<{
195
+ path: z.ZodArray<z.ZodUnion<[z.ZodString, z.ZodNumber]>, "many">;
196
+ message: z.ZodOptional<z.ZodString>;
197
+ code: z.ZodNativeEnum<{
198
+ invalid_type: "invalid_type";
199
+ invalid_literal: "invalid_literal";
200
+ custom: "custom";
201
+ invalid_union: "invalid_union";
202
+ invalid_union_discriminator: "invalid_union_discriminator";
203
+ invalid_enum_value: "invalid_enum_value";
204
+ unrecognized_keys: "unrecognized_keys";
205
+ invalid_arguments: "invalid_arguments";
206
+ invalid_return_type: "invalid_return_type";
207
+ invalid_date: "invalid_date";
208
+ invalid_string: "invalid_string";
209
+ too_small: "too_small";
210
+ too_big: "too_big";
211
+ invalid_intersection_types: "invalid_intersection_types";
212
+ not_multiple_of: "not_multiple_of";
213
+ not_finite: "not_finite";
214
+ }>;
215
+ }, z.ZodAny, "strip">, z.objectInputType<{
216
+ path: z.ZodArray<z.ZodUnion<[z.ZodString, z.ZodNumber]>, "many">;
217
+ message: z.ZodOptional<z.ZodString>;
218
+ code: z.ZodNativeEnum<{
219
+ invalid_type: "invalid_type";
220
+ invalid_literal: "invalid_literal";
221
+ custom: "custom";
222
+ invalid_union: "invalid_union";
223
+ invalid_union_discriminator: "invalid_union_discriminator";
224
+ invalid_enum_value: "invalid_enum_value";
225
+ unrecognized_keys: "unrecognized_keys";
226
+ invalid_arguments: "invalid_arguments";
227
+ invalid_return_type: "invalid_return_type";
228
+ invalid_date: "invalid_date";
229
+ invalid_string: "invalid_string";
230
+ too_small: "too_small";
231
+ too_big: "too_big";
232
+ invalid_intersection_types: "invalid_intersection_types";
233
+ not_multiple_of: "not_multiple_of";
234
+ not_finite: "not_finite";
235
+ }>;
236
+ }, z.ZodAny, "strip">>, "many">;
237
+ }, "strip", z.ZodTypeAny, {
238
+ name: "ZodError";
239
+ issues: z.objectOutputType<{
240
+ path: z.ZodArray<z.ZodUnion<[z.ZodString, z.ZodNumber]>, "many">;
241
+ message: z.ZodOptional<z.ZodString>;
242
+ code: z.ZodNativeEnum<{
243
+ invalid_type: "invalid_type";
244
+ invalid_literal: "invalid_literal";
245
+ custom: "custom";
246
+ invalid_union: "invalid_union";
247
+ invalid_union_discriminator: "invalid_union_discriminator";
248
+ invalid_enum_value: "invalid_enum_value";
249
+ unrecognized_keys: "unrecognized_keys";
250
+ invalid_arguments: "invalid_arguments";
251
+ invalid_return_type: "invalid_return_type";
252
+ invalid_date: "invalid_date";
253
+ invalid_string: "invalid_string";
254
+ too_small: "too_small";
255
+ too_big: "too_big";
256
+ invalid_intersection_types: "invalid_intersection_types";
257
+ not_multiple_of: "not_multiple_of";
258
+ not_finite: "not_finite";
259
+ }>;
260
+ }, z.ZodAny, "strip">[];
261
+ }, {
262
+ name: "ZodError";
263
+ issues: z.objectInputType<{
264
+ path: z.ZodArray<z.ZodUnion<[z.ZodString, z.ZodNumber]>, "many">;
265
+ message: z.ZodOptional<z.ZodString>;
266
+ code: z.ZodNativeEnum<{
267
+ invalid_type: "invalid_type";
268
+ invalid_literal: "invalid_literal";
269
+ custom: "custom";
270
+ invalid_union: "invalid_union";
271
+ invalid_union_discriminator: "invalid_union_discriminator";
272
+ invalid_enum_value: "invalid_enum_value";
273
+ unrecognized_keys: "unrecognized_keys";
274
+ invalid_arguments: "invalid_arguments";
275
+ invalid_return_type: "invalid_return_type";
276
+ invalid_date: "invalid_date";
277
+ invalid_string: "invalid_string";
278
+ too_small: "too_small";
279
+ too_big: "too_big";
280
+ invalid_intersection_types: "invalid_intersection_types";
281
+ not_multiple_of: "not_multiple_of";
282
+ not_finite: "not_finite";
283
+ }>;
284
+ }, z.ZodAny, "strip">[];
285
+ }>;
286
+ 401: z.ZodObject<{
287
+ code: z.ZodType<"UNAUTHORIZED", z.ZodTypeDef, "UNAUTHORIZED">;
288
+ error: z.ZodType<"Unauthorized", z.ZodTypeDef, "Unauthorized">;
289
+ name: z.ZodLiteral<"APIError">;
290
+ }, "strip", z.ZodTypeAny, {
291
+ code: "UNAUTHORIZED";
292
+ name: "APIError";
293
+ error: "Unauthorized";
294
+ }, {
295
+ code: "UNAUTHORIZED";
296
+ name: "APIError";
297
+ error: "Unauthorized";
298
+ }>;
299
+ 500: z.ZodObject<{
300
+ code: z.ZodType<"INTERNAL_SERVER_ERROR", z.ZodTypeDef, "INTERNAL_SERVER_ERROR">;
301
+ error: z.ZodType<string, z.ZodTypeDef, string>;
302
+ name: z.ZodLiteral<"APIError">;
303
+ }, "strip", z.ZodTypeAny, {
304
+ code: "INTERNAL_SERVER_ERROR";
305
+ name: "APIError";
306
+ error: string;
307
+ }, {
308
+ code: "INTERNAL_SERVER_ERROR";
309
+ name: "APIError";
310
+ error: string;
311
+ }>;
312
+ };
313
+ };
314
+ delete: {
315
+ method: "DELETE";
316
+ description: "Delete user avatar picture";
317
+ path: "/assets";
318
+ headers: z.ZodObject<{
319
+ authorization: z.ZodString;
320
+ }, "strip", z.ZodTypeAny, {
321
+ authorization: string;
322
+ }, {
323
+ authorization: string;
324
+ }>;
325
+ responses: {
326
+ 200: typeof import("@ts-rest/core").ContractNoBody;
327
+ 401: z.ZodObject<{
328
+ code: z.ZodType<"UNAUTHORIZED", z.ZodTypeDef, "UNAUTHORIZED">;
329
+ error: z.ZodType<"Unauthorized", z.ZodTypeDef, "Unauthorized">;
330
+ name: z.ZodLiteral<"APIError">;
331
+ }, "strip", z.ZodTypeAny, {
332
+ code: "UNAUTHORIZED";
333
+ name: "APIError";
334
+ error: "Unauthorized";
335
+ }, {
336
+ code: "UNAUTHORIZED";
337
+ name: "APIError";
338
+ error: "Unauthorized";
339
+ }>;
340
+ 500: z.ZodObject<{
341
+ code: z.ZodType<"INTERNAL_SERVER_ERROR", z.ZodTypeDef, "INTERNAL_SERVER_ERROR">;
342
+ error: z.ZodType<string, z.ZodTypeDef, string>;
343
+ name: z.ZodLiteral<"APIError">;
344
+ }, "strip", z.ZodTypeAny, {
345
+ code: "INTERNAL_SERVER_ERROR";
346
+ name: "APIError";
347
+ error: string;
348
+ }, {
349
+ code: "INTERNAL_SERVER_ERROR";
350
+ name: "APIError";
351
+ error: string;
352
+ }>;
353
+ };
354
+ };
355
+ };
356
+ export default _default;
@@ -0,0 +1,45 @@
1
+ import { initContract, ZodErrorSchema } from "@ts-rest/core";
2
+ import z from "zod";
3
+ import { apiError, apiSuccess } from "../types.js";
4
+ import { authHeaderSchema, cdnFeedbackHeaderSchema, authAssetTypeSchema, } from "../types/index.js";
5
+ const c = initContract();
6
+ export default c.router({
7
+ token: {
8
+ method: "POST",
9
+ path: "/assets/token",
10
+ headers: authHeaderSchema,
11
+ body: z.object({
12
+ type: authAssetTypeSchema,
13
+ }),
14
+ responses: {
15
+ 200: apiSuccess(z.object({
16
+ cdnToken: z.string(),
17
+ })),
18
+ 400: ZodErrorSchema,
19
+ 401: apiError(z.literal("UNAUTHORIZED"), z.literal("Unauthorized")),
20
+ },
21
+ },
22
+ update: {
23
+ method: "POST",
24
+ path: "/assets",
25
+ headers: cdnFeedbackHeaderSchema,
26
+ body: c.noBody(),
27
+ responses: {
28
+ 200: apiSuccess(c.noBody()),
29
+ 400: ZodErrorSchema,
30
+ 401: apiError(z.literal("UNAUTHORIZED"), z.literal("Unauthorized")),
31
+ 500: apiError(z.literal("INTERNAL_SERVER_ERROR"), z.string()),
32
+ },
33
+ },
34
+ delete: {
35
+ method: "DELETE",
36
+ path: "/assets",
37
+ description: "Delete user avatar picture",
38
+ headers: cdnFeedbackHeaderSchema,
39
+ responses: {
40
+ 200: apiSuccess(c.noBody()),
41
+ 401: apiError(z.literal("UNAUTHORIZED"), z.literal("Unauthorized")),
42
+ 500: apiError(z.literal("INTERNAL_SERVER_ERROR"), z.string()),
43
+ },
44
+ },
45
+ });