@adgytec/adgytec-web-utils 0.0.2 → 0.0.4

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.
Files changed (51) hide show
  1. package/dist/chunks/applicationError +28 -0
  2. package/dist/chunks/decode +42 -31
  3. package/dist/chunks/formParse +11 -0
  4. package/dist/chunks/parse +19 -82
  5. package/dist/chunks/schema +322 -0
  6. package/dist/chunks/upload +97 -95
  7. package/dist/constants/httpRequestCredentials.d.ts +1 -0
  8. package/dist/constants/httpRequestHeaders.d.ts +14 -6
  9. package/dist/constants/http_methods.d.ts +7 -0
  10. package/dist/constants/index.d.ts +2 -2
  11. package/dist/constants/index.js +27 -15
  12. package/dist/errorCodes/auth.d.ts +14 -0
  13. package/dist/errorCodes/common.d.ts +8 -0
  14. package/dist/errorCodes/form.d.ts +28 -0
  15. package/dist/errorCodes/iam.d.ts +6 -0
  16. package/dist/errorCodes/index.d.ts +8 -0
  17. package/dist/errorCodes/index.js +87 -0
  18. package/dist/errorCodes/media.d.ts +12 -0
  19. package/dist/errorCodes/pagination.d.ts +3 -0
  20. package/dist/errorCodes/reqBody.d.ts +6 -0
  21. package/dist/errorCodes/server.d.ts +7 -0
  22. package/dist/errorSchema/auth.d.ts +37 -0
  23. package/dist/errorSchema/common.d.ts +10 -0
  24. package/dist/errorSchema/form.d.ts +14 -0
  25. package/dist/errorSchema/formField.d.ts +117 -0
  26. package/dist/errorSchema/formFieldInvalid.d.ts +79 -0
  27. package/dist/errorSchema/formParse.d.ts +4 -0
  28. package/dist/errorSchema/iam.d.ts +21 -0
  29. package/dist/errorSchema/index.d.ts +12 -0
  30. package/dist/errorSchema/index.js +69 -0
  31. package/dist/errorSchema/media.d.ts +41 -0
  32. package/dist/errorSchema/pagination.d.ts +4 -0
  33. package/dist/errorSchema/reqBody.d.ts +35 -0
  34. package/dist/errorSchema/schema.d.ts +138 -0
  35. package/dist/errorSchema/server.d.ts +29 -0
  36. package/dist/errors/applicationError.d.ts +10 -0
  37. package/dist/errors/index.d.ts +1 -2
  38. package/dist/errors/index.js +5 -6
  39. package/dist/errors/parse.d.ts +15 -2
  40. package/dist/index.d.ts +2 -0
  41. package/dist/index.js +99 -33
  42. package/dist/media/types/lifecycle.d.ts +1 -2
  43. package/dist/response/decode.d.ts +1 -1
  44. package/dist/response/errorResponse.d.ts +1 -0
  45. package/dist/response/successReponse.d.ts +2 -0
  46. package/package.json +4 -2
  47. package/dist/chunks/apiError +0 -24
  48. package/dist/constants/http.d.ts +0 -5
  49. package/dist/constants/httpRequest.d.ts +0 -3
  50. package/dist/errors/apiError.d.ts +0 -8
  51. package/dist/errors/types.d.ts +0 -20
@@ -0,0 +1,28 @@
1
+ import "zod";
2
+ import { g as a } from "./schema";
3
+ class o extends Error {
4
+ }
5
+ class n extends o {
6
+ #s;
7
+ #r;
8
+ constructor(r, s = {}) {
9
+ super("application-error"), this.#s = r, this.#r = {
10
+ ...s,
11
+ code: r
12
+ };
13
+ }
14
+ get details() {
15
+ return this.#r;
16
+ }
17
+ get code() {
18
+ return this.#s;
19
+ }
20
+ parse() {
21
+ const { success: r, error: s, data: t } = a.safeParse(this.#r);
22
+ return r ? t : s;
23
+ }
24
+ }
25
+ export {
26
+ n as A,
27
+ o as B
28
+ };
@@ -1,39 +1,50 @@
1
- import { A as r, n as f } from "./apiError";
2
- import "zod";
3
- async function i(e, s) {
4
- const n = await e.text();
5
- if (!n) {
6
- if (e.ok) {
7
- if (s)
8
- throw new r(e, {
9
- message: "Expected response body but received empty response"
10
- });
11
- return null;
12
- }
13
- throw new r(e, {
14
- message: "Empty error response from server"
1
+ import a from "zod";
2
+ import { A as s } from "./applicationError";
3
+ import { serverCodes as t } from "../errorCodes/index.js";
4
+ function c(e, o) {
5
+ if (!e)
6
+ throw new s(t.invalidResponseShape, {
7
+ message: "Expected response body but received empty response",
8
+ payload: e
15
9
  });
16
- }
17
- let t;
10
+ const r = o.safeParse(e);
11
+ if (r.success) return r.data;
12
+ throw new s(t.invalidResponseShape, {
13
+ message: r.error.message,
14
+ payload: e
15
+ });
16
+ }
17
+ const i = a.object({
18
+ code: a.string()
19
+ }).loose();
20
+ function p(e, o) {
21
+ const r = i.safeParse(o);
22
+ throw r.success ? new s(r.data.code, r.data) : e >= 500 ? new s(t.internalServerError) : new s(t.unknownServerError, {
23
+ payload: o
24
+ });
25
+ }
26
+ async function u(e, o) {
27
+ if (!o && e.ok)
28
+ return null;
29
+ let r;
18
30
  try {
19
- t = JSON.parse(n);
31
+ r = await e.text();
20
32
  } catch {
21
- const p = e.ok ? "Malformed JSON response from server" : "Malformed error response from server";
22
- throw new r(e, { message: p });
33
+ throw new s(t.malformedResponseBody, {
34
+ response: e
35
+ });
23
36
  }
24
- if (e.ok) {
25
- if (!s)
26
- return null;
27
- const o = s.safeParse(t);
28
- if (!o.success)
29
- throw new r(e, {
30
- message: `Invalid response shape from server: ${o.error.message}`
37
+ let n;
38
+ if (r.length > 0)
39
+ try {
40
+ n = JSON.parse(r);
41
+ } catch {
42
+ throw new s(t.malformedJsonFromServer, {
43
+ response: e
31
44
  });
32
- return o.data;
33
- }
34
- const a = f(t);
35
- throw new r(e, a);
45
+ }
46
+ return e.ok ? c(n, o) : p(e.status, n);
36
47
  }
37
48
  export {
38
- i as d
49
+ u as d
39
50
  };
@@ -0,0 +1,11 @@
1
+ function t(s, n = "") {
2
+ const r = {};
3
+ for (const e of s) {
4
+ const o = n ? `${n}.${e.key}` : e.key;
5
+ "errors" in e ? r[o] = e.errors : Object.assign(r, t(e.children, o));
6
+ }
7
+ return r;
8
+ }
9
+ export {
10
+ t as f
11
+ };
package/dist/chunks/parse CHANGED
@@ -1,88 +1,25 @@
1
- import { A as o, B as n } from "./apiError";
2
- const d = 1e3, c = (e) => {
3
- if (e instanceof TypeError && (e.message === "Failed to fetch" || e.message === "NetworkError when attempting to fetch resource." || e.message === "Load failed" || // Safari
4
- e.message.includes("NetworkError") || e.message.includes("fetch")))
1
+ import e from "is-network-error";
2
+ import { commonCodes as t } from "../errorCodes/index.js";
3
+ import { A as n } from "./applicationError";
4
+ import i from "zod";
5
+ function p(r) {
6
+ if (e(r))
5
7
  return {
6
- errorCode: "network-error",
7
- message: "Check your network connection or try again later."
8
+ code: t.networkError,
9
+ debugMessage: r.toString()
8
10
  };
9
- if (e instanceof o) {
10
- if (e.response.status >= 500)
11
- return {
12
- errorCode: "server-error",
13
- message: e.data?.message ?? "Internal server error. Please try again later."
14
- };
15
- switch (e.response.status) {
16
- case 422:
17
- return {
18
- errorCode: "form-field-error",
19
- fieldErrors: e.data.fieldErrors ?? {}
20
- };
21
- case 400:
22
- return {
23
- errorCode: "user-action-error",
24
- message: e.data?.message ?? "Something went wrong while processing your request."
25
- };
26
- case 401:
27
- return {
28
- errorCode: "authentication-error",
29
- message: e.data?.message ?? "User login required."
30
- };
31
- case 403:
32
- return {
33
- errorCode: "authorization-error",
34
- message: e.data?.message ?? "You don't have necessary permissions to perform the requested action."
35
- };
36
- case 404:
37
- return {
38
- errorCode: "not-found-error",
39
- message: e.data?.message ?? "Requested action not found."
40
- };
41
- case 405:
42
- return {
43
- errorCode: "method-not-allowed-error",
44
- message: e.data?.message ?? "Requested action method not allowed."
45
- };
46
- case 429:
47
- const r = e.response.headers.get("retry-after");
48
- let s = d;
49
- if (r) {
50
- const t = parseInt(r, 10);
51
- if (!isNaN(t))
52
- s = t * 1e3;
53
- else {
54
- const a = new Date(r);
55
- isNaN(a.getTime()) || (s = Math.max(
56
- 0,
57
- a.getTime() - Date.now()
58
- ));
59
- }
60
- }
61
- return {
62
- errorCode: "too-many-requests-error",
63
- retryAfter: s,
64
- message: "Too many requests. Try again later."
65
- };
66
- case 413:
67
- return {
68
- errorCode: "content-too-large-error",
69
- message: e.data?.message ?? "Request content too large."
70
- };
71
- default:
72
- return {
73
- errorCode: "unknown-error",
74
- message: e.data?.message ?? "Unexpected error occurred."
75
- };
76
- }
11
+ if (r instanceof n) {
12
+ const o = r.parse();
13
+ return o instanceof i.ZodError ? {
14
+ code: t.zodError,
15
+ error: o
16
+ } : o;
77
17
  }
78
- return e instanceof n ? {
79
- errorCode: "unknown-error",
80
- message: e.message
81
- } : {
82
- errorCode: "unknown-error",
83
- message: "Unknown error occurred. Please refresh this page or try again later."
18
+ return {
19
+ code: t.unexpectedError,
20
+ debugMessage: r instanceof Error ? r.toString() : String(r)
84
21
  };
85
- };
22
+ }
86
23
  export {
87
- c as p
24
+ p
88
25
  };
@@ -0,0 +1,322 @@
1
+ import e from "zod";
2
+ import { serverCodes as c, formFieldInvalidTypeCauses as a, formFieldTypes as r, formCodes as b, authCodes as o, requestBodyCodes as n, paginationCodes as h, commonCodes as d, mediaCodes as t, iamCodes as l } from "../errorCodes/index.js";
3
+ const S = e.object({
4
+ code: e.literal(c.malformedResponseBody),
5
+ response: e.instanceof(Response)
6
+ }), p = e.object({
7
+ code: e.literal(c.malformedJsonFromServer),
8
+ response: e.instanceof(Response)
9
+ }), j = e.object({
10
+ code: e.literal(c.invalidResponseShape),
11
+ message: e.string(),
12
+ payload: e.unknown()
13
+ }).transform(({ code: s, message: i, payload: u }) => ({
14
+ code: s,
15
+ debugMessage: i,
16
+ payload: u
17
+ })), g = e.object({
18
+ code: e.literal(c.unknownServerError),
19
+ payload: e.unknown()
20
+ }), v = e.object({
21
+ code: e.literal(c.internalServerError)
22
+ }), y = e.object({
23
+ cause: e.literal(a.invalidValue)
24
+ }), E = e.object({
25
+ cause: e.literal(a.invalidEnumValue),
26
+ possibleValues: e.array(e.string())
27
+ }), f = e.object({
28
+ cause: e.literal(a.requireHttps)
29
+ }), F = e.object({
30
+ cause: e.literal(a.missingHost)
31
+ }), U = e.object({
32
+ cause: e.literal(a.containsPath)
33
+ }), N = e.object({
34
+ cause: e.literal(a.containsQuery)
35
+ }), I = e.object({
36
+ cause: e.literal(a.containsFragment)
37
+ }), M = e.object({
38
+ cause: e.literal(a.absoluteUrl)
39
+ }), w = e.object({
40
+ cause: e.literal(a.nilID)
41
+ }), A = e.object({
42
+ cause: e.literal(a.invalidEmail)
43
+ }), V = e.object({
44
+ cause: e.literal(a.missingMxRecords)
45
+ }), x = e.object({
46
+ cause: e.literal(a.notDigit)
47
+ }), B = e.object({
48
+ cause: e.literal(a.notBase64UrlEncoded)
49
+ }), C = e.object({
50
+ cause: e.literal(a.invalidUrl)
51
+ }), D = e.object({
52
+ cause: e.literal(a.nullValue)
53
+ }), R = e.discriminatedUnion(
54
+ "cause",
55
+ [
56
+ y,
57
+ E,
58
+ f,
59
+ F,
60
+ U,
61
+ N,
62
+ I,
63
+ M,
64
+ w,
65
+ A,
66
+ V,
67
+ x,
68
+ B,
69
+ C,
70
+ D
71
+ ]
72
+ ), k = e.object({
73
+ type: e.literal(r.unknown)
74
+ }), q = e.object({
75
+ type: e.literal(r.missing)
76
+ }), z = e.object({
77
+ type: e.literal(r.overflow),
78
+ details: e.object({
79
+ max: e.union([e.coerce.date(), e.number()])
80
+ })
81
+ }), O = e.object({
82
+ type: e.literal(r.underflow),
83
+ details: e.object({
84
+ min: e.union([e.coerce.date(), e.number()])
85
+ })
86
+ }), P = e.object({
87
+ type: e.literal(r.length),
88
+ details: e.object({
89
+ min: e.number(),
90
+ max: e.number()
91
+ })
92
+ }), T = e.object({
93
+ type: e.literal(r.invalid),
94
+ details: R
95
+ }), L = e.discriminatedUnion("type", [
96
+ k,
97
+ q,
98
+ z,
99
+ O,
100
+ P,
101
+ T
102
+ ]), m = e.lazy(
103
+ () => e.union([
104
+ e.object({
105
+ key: e.string(),
106
+ errors: e.array(L)
107
+ }),
108
+ e.object({
109
+ key: e.string(),
110
+ children: e.array(m)
111
+ })
112
+ ])
113
+ ), H = e.object({
114
+ code: e.literal(b.formValidationFailed),
115
+ details: e.array(m)
116
+ }), J = e.object({
117
+ code: e.literal(o.invalidApiKey)
118
+ }), K = e.object({
119
+ code: e.literal(o.userNotFound)
120
+ }), Q = e.object({
121
+ code: e.literal(o.jwtNotAcceptable)
122
+ }), G = e.object({
123
+ code: e.literal(o.invalidSignedUrl)
124
+ }), W = e.object({
125
+ code: e.literal(o.hashMismatch)
126
+ }), X = e.object({
127
+ code: e.literal(o.invalidAuthHeaderValue)
128
+ }), Y = e.object({
129
+ code: e.literal(o.unsupportedAuthScheme)
130
+ }), Z = e.object({
131
+ code: e.literal(o.organizationStatusBad)
132
+ }), _ = e.object({
133
+ code: e.literal(o.userNotExistsInOrganizationManagement)
134
+ }), $ = e.object({
135
+ code: e.literal(o.userNotExistInOrganization)
136
+ }), ee = e.object({
137
+ code: e.literal(o.userDisabled)
138
+ }), ae = e.object({
139
+ code: e.literal(o.tokenNotFound)
140
+ }), oe = e.object({
141
+ code: e.literal(n.invalidRequestBody),
142
+ message: e.string()
143
+ }).transform(({ code: s, message: i }) => ({
144
+ code: s,
145
+ debugMessage: i
146
+ })), te = e.object({
147
+ code: e.literal(n.unknownFieldInRequestBody),
148
+ message: e.string()
149
+ }).transform(({ code: s, message: i }) => ({
150
+ code: s,
151
+ debugMessage: i
152
+ })), se = e.object({
153
+ code: e.literal(n.requestBodyTooLarge),
154
+ limit: e.int()
155
+ }), ie = e.object({
156
+ code: e.literal(n.emptyRequestBody),
157
+ message: e.string()
158
+ }).transform(({ code: s, message: i }) => ({
159
+ code: s,
160
+ debugMessage: i
161
+ })), re = e.object({
162
+ code: e.literal(h.invalidCursorValue)
163
+ }), ce = e.object({
164
+ code: e.literal(d.invalidId)
165
+ }), ne = e.object({
166
+ code: e.literal(d.routeNotFound)
167
+ }), le = e.object({
168
+ code: e.literal(d.methodNotAllowed)
169
+ }), de = e.object({
170
+ code: e.literal(t.invalidMultipartNumber)
171
+ }), me = e.object({
172
+ code: e.literal(t.mediaObjectNotFound)
173
+ }), ue = e.object({
174
+ code: e.literal(t.mediaTooLarge),
175
+ mediaID: e.uuidv7(),
176
+ currentSize: e.int(),
177
+ maxSupportedSize: e.int()
178
+ }), be = e.object({
179
+ code: e.literal(t.mediaItemsLimitExceeded),
180
+ currentLength: e.int(),
181
+ maxItemsSupported: e.int()
182
+ }), he = e.object({
183
+ code: e.literal(t.uploadAlreadyCompleted)
184
+ }), Se = e.object({
185
+ code: e.literal(t.unsupportedObjectUploaded)
186
+ }), pe = e.object({
187
+ code: e.literal(t.completeMultipartUploadCalledTooSoon)
188
+ }), je = e.object({
189
+ code: e.literal(t.singlepartUploadFailed),
190
+ mediaID: e.uuidv7()
191
+ }), ge = e.object({
192
+ code: e.literal(t.multipartPartUploadFailed),
193
+ mediaID: e.uuidv7(),
194
+ partNumber: e.int()
195
+ }), ve = e.object({
196
+ code: e.literal(t.missingETagValue),
197
+ mediaID: e.uuidv7(),
198
+ partNumber: e.int()
199
+ }), ye = e.object({
200
+ code: e.literal(l.selfPermissionMismatch),
201
+ permission: e.string(),
202
+ key: e.string(),
203
+ required: e.string(),
204
+ got: e.string()
205
+ }), Ee = e.object({
206
+ code: e.literal(l.invalidActor),
207
+ supportedActors: e.array(e.string()),
208
+ currentActor: e.string()
209
+ }), fe = e.object({
210
+ code: e.literal(l.permissionExplicitlyDenied),
211
+ deniedPermission: e.string()
212
+ }), Fe = e.object({
213
+ code: e.literal(l.missingPermission),
214
+ missingPermission: e.string()
215
+ }), Ie = e.discriminatedUnion("code", [
216
+ J,
217
+ K,
218
+ Q,
219
+ G,
220
+ W,
221
+ X,
222
+ Z,
223
+ _,
224
+ $,
225
+ ee,
226
+ ae,
227
+ Y,
228
+ ce,
229
+ ne,
230
+ le,
231
+ H,
232
+ ye,
233
+ Ee,
234
+ fe,
235
+ Fe,
236
+ de,
237
+ me,
238
+ ue,
239
+ be,
240
+ he,
241
+ Se,
242
+ pe,
243
+ je,
244
+ ge,
245
+ ve,
246
+ re,
247
+ oe,
248
+ te,
249
+ se,
250
+ ie,
251
+ S,
252
+ p,
253
+ j,
254
+ g,
255
+ v
256
+ ]);
257
+ export {
258
+ je as $,
259
+ oe as A,
260
+ j as B,
261
+ G as C,
262
+ C as D,
263
+ y as E,
264
+ Q as F,
265
+ p as G,
266
+ S as H,
267
+ be as I,
268
+ me as J,
269
+ ue as K,
270
+ le as L,
271
+ ve as M,
272
+ F as N,
273
+ V as O,
274
+ Fe as P,
275
+ ge as Q,
276
+ w as R,
277
+ B as S,
278
+ x as T,
279
+ D as U,
280
+ Z as V,
281
+ fe as W,
282
+ se as X,
283
+ f as Y,
284
+ ne as Z,
285
+ ye as _,
286
+ M as a,
287
+ ae as a0,
288
+ te as a1,
289
+ g as a2,
290
+ Y as a3,
291
+ Se as a4,
292
+ he as a5,
293
+ ee as a6,
294
+ $ as a7,
295
+ _ as a8,
296
+ K as a9,
297
+ I as b,
298
+ pe as c,
299
+ U as d,
300
+ N as e,
301
+ ie as f,
302
+ Ie as g,
303
+ T as h,
304
+ P as i,
305
+ q as j,
306
+ z as k,
307
+ O as l,
308
+ k as m,
309
+ L as n,
310
+ R as o,
311
+ H as p,
312
+ W as q,
313
+ v as r,
314
+ Ee as s,
315
+ J as t,
316
+ X as u,
317
+ re as v,
318
+ A as w,
319
+ E as x,
320
+ ce as y,
321
+ de as z
322
+ };