authula 0.5.4 → 0.7.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.
@@ -0,0 +1,18 @@
1
+ //#region \0rolldown/runtime.js
2
+ var __defProp = Object.defineProperty;
3
+ var __exportAll = (all, no_symbols) => {
4
+ let target = {};
5
+ for (var name in all) {
6
+ __defProp(target, name, {
7
+ get: all[name],
8
+ enumerable: true
9
+ });
10
+ }
11
+ if (!no_symbols) {
12
+ __defProp(target, Symbol.toStringTag, { value: "Module" });
13
+ }
14
+ return target;
15
+ };
16
+
17
+ //#endregion
18
+ export { __exportAll as t };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "authula",
3
3
  "type": "module",
4
- "version": "0.5.4",
4
+ "version": "0.7.0",
5
5
  "description": "Official SDK for Authula.",
6
6
  "license": "Apache-2.0",
7
7
  "homepage": "https://github.com/Authula/authula-node-sdk#readme",
@@ -13,41 +13,42 @@
13
13
  "url": "https://github.com/Authula/authula-node-sdk/issues"
14
14
  },
15
15
  "exports": {
16
- ".": {
17
- "import": "./dist/index.js",
18
- "require": "./dist/index.cjs"
19
- },
20
- "./plugins": {
21
- "import": "./dist/plugins/index.js",
22
- "require": "./dist/plugins/index.cjs"
23
- },
16
+ ".": "./dist/index.js",
17
+ "./plugins": "./dist/plugins/index.js",
24
18
  "./package.json": "./package.json"
25
19
  },
26
- "main": "./dist/index.cjs",
27
- "module": "./dist/index.js",
28
20
  "types": "./dist/index.d.cts",
29
21
  "files": [
30
22
  "dist"
31
23
  ],
32
- "packageManager": "pnpm@11.9.0",
24
+ "packageManager": "pnpm@11.10.0",
33
25
  "scripts": {
34
- "build": "tsdown",
35
- "dev": "tsdown --watch",
26
+ "orval:build": "rm -rf ./src/gen && orval -c orval.config.ts",
27
+ "tsdown:build": "rm -rf ./dist && tsdown",
28
+ "watch:orval": "orval",
29
+ "watch:tsdown": "tsdown --watch",
36
30
  "typecheck": "tsc --noEmit",
31
+ "format": "biome format --write",
37
32
  "test": "vitest run",
38
33
  "test:watch": "vitest --watch",
39
- "prepublishOnly": "pnpm run build"
34
+ "prepublishOnly": "pnpm run orval:build && pnpm run tsdown:build"
40
35
  },
41
36
  "dependencies": {
37
+ "@tanstack/react-query": "^5.101.2",
42
38
  "cookie": "^2.0.1",
43
39
  "es-toolkit": "^1.49.0",
44
40
  "zod": "^4.4.3"
45
41
  },
46
42
  "devDependencies": {
47
- "@types/node": "^26.1.0",
43
+ "@biomejs/biome": "^2.5.3",
44
+ "@faker-js/faker": "^10.5.0",
45
+ "@types/node": "^26.1.1",
46
+ "@vitest/coverage-v8": "^4.1.10",
47
+ "@vitest/ui": "^4.1.10",
48
48
  "bumpp": "^11.1.0",
49
- "tsdown": "^0.22.3",
49
+ "orval": "^8.20.0",
50
+ "tsdown": "^0.22.4",
50
51
  "typescript": "^6.0.3",
51
- "vitest": "^4.1.9"
52
+ "vitest": "^4.1.10"
52
53
  }
53
54
  }
@@ -1,128 +0,0 @@
1
- import { z } from "zod";
2
- import { toCamelCaseKeys, toSnakeCaseKeys } from "es-toolkit";
3
-
4
- //#region src/types/schemas.ts
5
- const userSchema = z.object({
6
- id: z.uuid(),
7
- name: z.string().nonempty(),
8
- email: z.email(),
9
- emailVerified: z.boolean(),
10
- image: z.string().nullish(),
11
- metadata: z.record(z.string(), z.unknown()).nullish(),
12
- createdAt: z.iso.datetime(),
13
- updatedAt: z.iso.datetime()
14
- });
15
- const accountSchema = z.object({
16
- id: z.uuid(),
17
- userId: z.string().nonempty(),
18
- accountId: z.string().nonempty(),
19
- providerId: z.string().nonempty(),
20
- accessToken: z.string().nullish(),
21
- refreshToken: z.string().nullish(),
22
- idToken: z.string().nullish(),
23
- accessTokenExpiresAt: z.iso.datetime().nullish(),
24
- refreshTokenExpiresAt: z.iso.datetime().nullish(),
25
- scope: z.string().nullish(),
26
- password: z.string().nullish(),
27
- createdAt: z.iso.datetime(),
28
- updatedAt: z.iso.datetime()
29
- });
30
- const sessionSchema = z.object({
31
- id: z.uuid(),
32
- userId: z.string().nonempty(),
33
- token: z.string().nonempty(),
34
- expiresAt: z.iso.datetime(),
35
- ipAddress: z.string().nullish(),
36
- userAgent: z.string().nullish(),
37
- createdAt: z.iso.datetime(),
38
- updatedAt: z.iso.datetime()
39
- });
40
- const verificationTypeSchema = z.enum([
41
- "email_verification",
42
- "password_reset_request",
43
- "email_reset_request"
44
- ]);
45
- const verificationSchema = z.object({
46
- id: z.uuid(),
47
- userId: z.string().nullish(),
48
- identifier: z.string().nonempty(),
49
- token: z.string().nonempty(),
50
- type: verificationTypeSchema,
51
- expiresAt: z.iso.datetime(),
52
- createdAt: z.iso.datetime(),
53
- updatedAt: z.iso.datetime()
54
- });
55
-
56
- //#endregion
57
- //#region src/fetch.ts
58
- async function wrappedFetch(client, endpoint, options = {}) {
59
- const headersCombined = {
60
- ...options.headers,
61
- ...client.config.fetchOptions?.headers ?? {}
62
- };
63
- const headers = new Headers(headersCombined || {});
64
- let cookieStore = null;
65
- if (client.config.cookies) {
66
- cookieStore = await client.config.cookies();
67
- const cookieHeader = cookieStore.getAll().map((c) => `${c.name}=${c.value}`).join("; ");
68
- if (cookieHeader) headers.set("cookie", cookieHeader);
69
- }
70
- const controller = new AbortController();
71
- const abortTimeout = client.config.fetchOptions?.abortTimeout;
72
- if (abortTimeout) setTimeout(() => controller.abort(), abortTimeout * 1e3);
73
- const ctx = {
74
- url: `${client.config.url}${endpoint}`,
75
- init: {
76
- method: options.method ?? "GET",
77
- headers,
78
- body: options.body ? JSON.stringify(toSnakeCaseKeys(options.body)) : void 0,
79
- credentials: "include",
80
- signal: controller.signal
81
- },
82
- meta: {}
83
- };
84
- await client.runBeforeFetch(ctx);
85
- let res = await fetch(ctx.url, ctx.init);
86
- if (await client.runAfterFetch(ctx, res) === "retry" && !ctx.meta.retry) {
87
- ctx.meta.retry = true;
88
- res = await fetch(ctx.url, ctx.init);
89
- }
90
- const setCookieHeaders = res.headers.getSetCookie?.() ?? res.headers.get("set-cookie")?.split(", ") ?? null;
91
- if (!res.ok) throw new Error(await res.text());
92
- const resData = await res.json();
93
- if (cookieStore && setCookieHeaders) for (const raw of setCookieHeaders) {
94
- const [pair, ...attrs] = raw.split(";");
95
- const [name, value] = pair.split("=");
96
- if (!name || !value) continue;
97
- const options = {};
98
- for (const attr of attrs) {
99
- const [k, v] = attr.trim().split("=");
100
- switch (k.toLowerCase()) {
101
- case "path":
102
- options.path = v;
103
- break;
104
- case "expires":
105
- options.expires = new Date(v);
106
- break;
107
- case "max-age":
108
- options.maxAge = Number(v);
109
- break;
110
- case "httponly":
111
- options.httpOnly = true;
112
- break;
113
- case "secure":
114
- options.secure = true;
115
- break;
116
- case "samesite":
117
- options.sameSite = v?.toLowerCase();
118
- break;
119
- }
120
- }
121
- cookieStore.set(name.trim(), decodeURIComponent(value.trim()), options);
122
- }
123
- return toCamelCaseKeys(resData);
124
- }
125
-
126
- //#endregion
127
- export { verificationSchema as a, userSchema as i, accountSchema as n, verificationTypeSchema as o, sessionSchema as r, wrappedFetch as t };
128
- //# sourceMappingURL=fetch-BnZpQ0Yq.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"fetch-BnZpQ0Yq.js","names":[],"sources":["../src/types/schemas.ts","../src/fetch.ts"],"sourcesContent":["import { z } from \"zod\";\n\nexport const userSchema = z.object({\n id: z.uuid(),\n name: z.string().nonempty(),\n email: z.email(),\n emailVerified: z.boolean(),\n image: z.string().nullish(),\n metadata: z.record(z.string(), z.unknown()).nullish(),\n createdAt: z.iso.datetime(),\n updatedAt: z.iso.datetime(),\n});\nexport type User = z.infer<typeof userSchema>;\n\nexport const accountSchema = z.object({\n id: z.uuid(),\n userId: z.string().nonempty(),\n accountId: z.string().nonempty(),\n providerId: z.string().nonempty(),\n accessToken: z.string().nullish(),\n refreshToken: z.string().nullish(),\n idToken: z.string().nullish(),\n accessTokenExpiresAt: z.iso.datetime().nullish(),\n refreshTokenExpiresAt: z.iso.datetime().nullish(),\n scope: z.string().nullish(),\n password: z.string().nullish(),\n createdAt: z.iso.datetime(),\n updatedAt: z.iso.datetime(),\n});\nexport type Account = z.infer<typeof accountSchema>;\n\nexport const sessionSchema = z.object({\n id: z.uuid(),\n userId: z.string().nonempty(),\n token: z.string().nonempty(),\n expiresAt: z.iso.datetime(),\n ipAddress: z.string().nullish(),\n userAgent: z.string().nullish(),\n createdAt: z.iso.datetime(),\n updatedAt: z.iso.datetime(),\n});\nexport type Session = z.infer<typeof sessionSchema>;\n\nexport const verificationTypeSchema = z.enum([\n \"email_verification\",\n \"password_reset_request\",\n \"email_reset_request\",\n]);\nexport type VerificationType = z.infer<typeof verificationTypeSchema>;\n\nexport const verificationSchema = z.object({\n id: z.uuid(),\n userId: z.string().nullish(),\n identifier: z.string().nonempty(),\n token: z.string().nonempty(),\n type: verificationTypeSchema,\n expiresAt: z.iso.datetime(),\n createdAt: z.iso.datetime(),\n updatedAt: z.iso.datetime(),\n});\nexport type Verification = z.infer<typeof verificationSchema>;\n","import { toCamelCaseKeys, toSnakeCaseKeys } from \"es-toolkit\";\n\nimport type { AuthulaClient } from \"./client\";\nimport type { CookieStore, FetchContext, FetchRequestOptions } from \"./types\";\n\nexport async function wrappedFetch<T>(\n client: AuthulaClient,\n endpoint: string,\n options: FetchRequestOptions = {},\n): Promise<T> {\n const headersCombined = {\n ...options.headers,\n ...(client.config.fetchOptions?.headers ?? {}),\n };\n const headers = new Headers(headersCombined || {});\n\n let cookieStore: CookieStore | null = null;\n\n if (client.config.cookies) {\n cookieStore = await client.config.cookies();\n\n const cookieHeader = cookieStore\n .getAll()\n .map((c) => `${c.name}=${c.value}`)\n .join(\"; \");\n\n if (cookieHeader) {\n headers.set(\"cookie\", cookieHeader);\n }\n }\n\n const controller = new AbortController();\n const abortTimeout = client.config.fetchOptions?.abortTimeout;\n\n if (abortTimeout) {\n setTimeout(() => controller.abort(), abortTimeout * 1000);\n }\n\n const ctx: FetchContext = {\n url: `${client.config.url}${endpoint}`,\n init: {\n method: options.method ?? \"GET\",\n headers,\n body: options.body\n ? JSON.stringify(toSnakeCaseKeys(options.body))\n : undefined,\n credentials: \"include\",\n signal: controller.signal,\n },\n meta: {},\n };\n\n await client.runBeforeFetch(ctx);\n\n let res = await fetch(ctx.url, ctx.init);\n\n const action = await client.runAfterFetch(ctx, res);\n if (action === \"retry\" && !ctx.meta.retry) {\n ctx.meta.retry = true;\n res = await fetch(ctx.url, ctx.init);\n }\n\n const setCookieHeaders =\n res.headers.getSetCookie?.() ??\n res.headers.get(\"set-cookie\")?.split(\", \") ??\n null;\n\n if (!res.ok) {\n throw new Error(await res.text());\n }\n\n const resData = await res.json();\n\n // Apply Set-Cookie back into Next.js store (SSR only)\n if (cookieStore && setCookieHeaders) {\n for (const raw of setCookieHeaders) {\n const [pair, ...attrs] = raw.split(\";\");\n const [name, value] = pair.split(\"=\");\n\n if (!name || !value) continue;\n\n const options: Record<string, any> = {};\n\n for (const attr of attrs) {\n const [k, v] = attr.trim().split(\"=\");\n\n switch (k.toLowerCase()) {\n case \"path\":\n options.path = v;\n break;\n case \"expires\":\n options.expires = new Date(v);\n break;\n case \"max-age\":\n options.maxAge = Number(v);\n break;\n case \"httponly\":\n options.httpOnly = true;\n break;\n case \"secure\":\n options.secure = true;\n break;\n case \"samesite\":\n options.sameSite = v?.toLowerCase();\n break;\n }\n }\n\n cookieStore.set(name.trim(), decodeURIComponent(value.trim()), options);\n }\n }\n\n return toCamelCaseKeys(resData) as T;\n}\n"],"mappings":";;;;AAEA,MAAa,aAAa,EAAE,OAAO;CACjC,IAAI,EAAE,KAAK;CACX,MAAM,EAAE,OAAO,CAAC,CAAC,SAAS;CAC1B,OAAO,EAAE,MAAM;CACf,eAAe,EAAE,QAAQ;CACzB,OAAO,EAAE,OAAO,CAAC,CAAC,QAAQ;CAC1B,UAAU,EAAE,OAAO,EAAE,OAAO,GAAG,EAAE,QAAQ,CAAC,CAAC,CAAC,QAAQ;CACpD,WAAW,EAAE,IAAI,SAAS;CAC1B,WAA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C;CAEF,IAAI,CAAC,IAAI,IACP,MAAM,IAAI,MAAM,MAAM,IAAI,KAAK,CAAC;CAGlC,MAAM,UAAU,MAAM,IAAI,KAAK;CAG/B,IAAI,eAAe,kBACjB,KAAK,MAAM,OAAO,kBAAkB;EAClC,MAAM,CAAC,MAAM,GAAG,SAAS,IAAI,MAAM,GAAG;EACtC,MAAM,CAAC,MAAM,SAAS,KAAK,MAAM,GAAG;EAEpC,IAAI,CAAC,QAAQ,CAAC,OAAO;EAErB,MAAM,UAA+B,CAAC;EAEtC,KAAK,MAAM,QAAQ,OAAO;GACxB,MAAM,CAAC,GAAG,KAAK,KAAK,KAAK,CAAC,CAAC,MAAM,GAAG;GAEpC,QAAQ,EAAE,YAAY,GAAtB;IACE,KAAK;KACH,QAAQ,OAAO;KACf;IACF,KAAK;KACH,QAAQ,UAAU,IAAI,KAAK,CAAC;KAC5B;IACF,KAAK;KACH,QAAQ,SAAS,OAAO,CAAC;KACzB;IACF,KAAK;KACH,QAAQ,WAAW;KACnB;IACF,KAAK;KACH,QAAQ,SAAS;KACjB;IACF,KAAK;KACH,QAAQ,WAAW,GAAG,YAAY;KAClC;GACJ;EACF;EAEA,YAAY,IAAI,KAAK,KAAK,GAAG,mBAAmB,MAAM,KAAK,CAAC,GAAG,OAAO;CACxE;CAGF,OAAO,gBAAgB,OAAO;AAChC"}
@@ -1,163 +0,0 @@
1
- let zod = require("zod");
2
- let es_toolkit = require("es-toolkit");
3
-
4
- //#region src/types/schemas.ts
5
- const userSchema = zod.z.object({
6
- id: zod.z.uuid(),
7
- name: zod.z.string().nonempty(),
8
- email: zod.z.email(),
9
- emailVerified: zod.z.boolean(),
10
- image: zod.z.string().nullish(),
11
- metadata: zod.z.record(zod.z.string(), zod.z.unknown()).nullish(),
12
- createdAt: zod.z.iso.datetime(),
13
- updatedAt: zod.z.iso.datetime()
14
- });
15
- const accountSchema = zod.z.object({
16
- id: zod.z.uuid(),
17
- userId: zod.z.string().nonempty(),
18
- accountId: zod.z.string().nonempty(),
19
- providerId: zod.z.string().nonempty(),
20
- accessToken: zod.z.string().nullish(),
21
- refreshToken: zod.z.string().nullish(),
22
- idToken: zod.z.string().nullish(),
23
- accessTokenExpiresAt: zod.z.iso.datetime().nullish(),
24
- refreshTokenExpiresAt: zod.z.iso.datetime().nullish(),
25
- scope: zod.z.string().nullish(),
26
- password: zod.z.string().nullish(),
27
- createdAt: zod.z.iso.datetime(),
28
- updatedAt: zod.z.iso.datetime()
29
- });
30
- const sessionSchema = zod.z.object({
31
- id: zod.z.uuid(),
32
- userId: zod.z.string().nonempty(),
33
- token: zod.z.string().nonempty(),
34
- expiresAt: zod.z.iso.datetime(),
35
- ipAddress: zod.z.string().nullish(),
36
- userAgent: zod.z.string().nullish(),
37
- createdAt: zod.z.iso.datetime(),
38
- updatedAt: zod.z.iso.datetime()
39
- });
40
- const verificationTypeSchema = zod.z.enum([
41
- "email_verification",
42
- "password_reset_request",
43
- "email_reset_request"
44
- ]);
45
- const verificationSchema = zod.z.object({
46
- id: zod.z.uuid(),
47
- userId: zod.z.string().nullish(),
48
- identifier: zod.z.string().nonempty(),
49
- token: zod.z.string().nonempty(),
50
- type: verificationTypeSchema,
51
- expiresAt: zod.z.iso.datetime(),
52
- createdAt: zod.z.iso.datetime(),
53
- updatedAt: zod.z.iso.datetime()
54
- });
55
-
56
- //#endregion
57
- //#region src/fetch.ts
58
- async function wrappedFetch(client, endpoint, options = {}) {
59
- const headersCombined = {
60
- ...options.headers,
61
- ...client.config.fetchOptions?.headers ?? {}
62
- };
63
- const headers = new Headers(headersCombined || {});
64
- let cookieStore = null;
65
- if (client.config.cookies) {
66
- cookieStore = await client.config.cookies();
67
- const cookieHeader = cookieStore.getAll().map((c) => `${c.name}=${c.value}`).join("; ");
68
- if (cookieHeader) headers.set("cookie", cookieHeader);
69
- }
70
- const controller = new AbortController();
71
- const abortTimeout = client.config.fetchOptions?.abortTimeout;
72
- if (abortTimeout) setTimeout(() => controller.abort(), abortTimeout * 1e3);
73
- const ctx = {
74
- url: `${client.config.url}${endpoint}`,
75
- init: {
76
- method: options.method ?? "GET",
77
- headers,
78
- body: options.body ? JSON.stringify((0, es_toolkit.toSnakeCaseKeys)(options.body)) : void 0,
79
- credentials: "include",
80
- signal: controller.signal
81
- },
82
- meta: {}
83
- };
84
- await client.runBeforeFetch(ctx);
85
- let res = await fetch(ctx.url, ctx.init);
86
- if (await client.runAfterFetch(ctx, res) === "retry" && !ctx.meta.retry) {
87
- ctx.meta.retry = true;
88
- res = await fetch(ctx.url, ctx.init);
89
- }
90
- const setCookieHeaders = res.headers.getSetCookie?.() ?? res.headers.get("set-cookie")?.split(", ") ?? null;
91
- if (!res.ok) throw new Error(await res.text());
92
- const resData = await res.json();
93
- if (cookieStore && setCookieHeaders) for (const raw of setCookieHeaders) {
94
- const [pair, ...attrs] = raw.split(";");
95
- const [name, value] = pair.split("=");
96
- if (!name || !value) continue;
97
- const options = {};
98
- for (const attr of attrs) {
99
- const [k, v] = attr.trim().split("=");
100
- switch (k.toLowerCase()) {
101
- case "path":
102
- options.path = v;
103
- break;
104
- case "expires":
105
- options.expires = new Date(v);
106
- break;
107
- case "max-age":
108
- options.maxAge = Number(v);
109
- break;
110
- case "httponly":
111
- options.httpOnly = true;
112
- break;
113
- case "secure":
114
- options.secure = true;
115
- break;
116
- case "samesite":
117
- options.sameSite = v?.toLowerCase();
118
- break;
119
- }
120
- }
121
- cookieStore.set(name.trim(), decodeURIComponent(value.trim()), options);
122
- }
123
- return (0, es_toolkit.toCamelCaseKeys)(resData);
124
- }
125
-
126
- //#endregion
127
- Object.defineProperty(exports, 'accountSchema', {
128
- enumerable: true,
129
- get: function () {
130
- return accountSchema;
131
- }
132
- });
133
- Object.defineProperty(exports, 'sessionSchema', {
134
- enumerable: true,
135
- get: function () {
136
- return sessionSchema;
137
- }
138
- });
139
- Object.defineProperty(exports, 'userSchema', {
140
- enumerable: true,
141
- get: function () {
142
- return userSchema;
143
- }
144
- });
145
- Object.defineProperty(exports, 'verificationSchema', {
146
- enumerable: true,
147
- get: function () {
148
- return verificationSchema;
149
- }
150
- });
151
- Object.defineProperty(exports, 'verificationTypeSchema', {
152
- enumerable: true,
153
- get: function () {
154
- return verificationTypeSchema;
155
- }
156
- });
157
- Object.defineProperty(exports, 'wrappedFetch', {
158
- enumerable: true,
159
- get: function () {
160
- return wrappedFetch;
161
- }
162
- });
163
- //# sourceMappingURL=fetch-CEj1gBkL.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"fetch-CEj1gBkL.cjs","names":["z"],"sources":["../src/types/schemas.ts","../src/fetch.ts"],"sourcesContent":["import { z } from \"zod\";\n\nexport const userSchema = z.object({\n id: z.uuid(),\n name: z.string().nonempty(),\n email: z.email(),\n emailVerified: z.boolean(),\n image: z.string().nullish(),\n metadata: z.record(z.string(), z.unknown()).nullish(),\n createdAt: z.iso.datetime(),\n updatedAt: z.iso.datetime(),\n});\nexport type User = z.infer<typeof userSchema>;\n\nexport const accountSchema = z.object({\n id: z.uuid(),\n userId: z.string().nonempty(),\n accountId: z.string().nonempty(),\n providerId: z.string().nonempty(),\n accessToken: z.string().nullish(),\n refreshToken: z.string().nullish(),\n idToken: z.string().nullish(),\n accessTokenExpiresAt: z.iso.datetime().nullish(),\n refreshTokenExpiresAt: z.iso.datetime().nullish(),\n scope: z.string().nullish(),\n password: z.string().nullish(),\n createdAt: z.iso.datetime(),\n updatedAt: z.iso.datetime(),\n});\nexport type Account = z.infer<typeof accountSchema>;\n\nexport const sessionSchema = z.object({\n id: z.uuid(),\n userId: z.string().nonempty(),\n token: z.string().nonempty(),\n expiresAt: z.iso.datetime(),\n ipAddress: z.string().nullish(),\n userAgent: z.string().nullish(),\n createdAt: z.iso.datetime(),\n updatedAt: z.iso.datetime(),\n});\nexport type Session = z.infer<typeof sessionSchema>;\n\nexport const verificationTypeSchema = z.enum([\n \"email_verification\",\n \"password_reset_request\",\n \"email_reset_request\",\n]);\nexport type VerificationType = z.infer<typeof verificationTypeSchema>;\n\nexport const verificationSchema = z.object({\n id: z.uuid(),\n userId: z.string().nullish(),\n identifier: z.string().nonempty(),\n token: z.string().nonempty(),\n type: verificationTypeSchema,\n expiresAt: z.iso.datetime(),\n createdAt: z.iso.datetime(),\n updatedAt: z.iso.datetime(),\n});\nexport type Verification = z.infer<typeof verificationSchema>;\n","import { toCamelCaseKeys, toSnakeCaseKeys } from \"es-toolkit\";\n\nimport type { AuthulaClient } from \"./client\";\nimport type { CookieStore, FetchContext, FetchRequestOptions } from \"./types\";\n\nexport async function wrappedFetch<T>(\n client: AuthulaClient,\n endpoint: string,\n options: FetchRequestOptions = {},\n): Promise<T> {\n const headersCombined = {\n ...options.headers,\n ...(client.config.fetchOptions?.headers ?? {}),\n };\n const headers = new Headers(headersCombined || {});\n\n let cookieStore: CookieStore | null = null;\n\n if (client.config.cookies) {\n cookieStore = await client.config.cookies();\n\n const cookieHeader = cookieStore\n .getAll()\n .map((c) => `${c.name}=${c.value}`)\n .join(\"; \");\n\n if (cookieHeader) {\n headers.set(\"cookie\", cookieHeader);\n }\n }\n\n const controller = new AbortController();\n const abortTimeout = client.config.fetchOptions?.abortTimeout;\n\n if (abortTimeout) {\n setTimeout(() => controller.abort(), abortTimeout * 1000);\n }\n\n const ctx: FetchContext = {\n url: `${client.config.url}${endpoint}`,\n init: {\n method: options.method ?? \"GET\",\n headers,\n body: options.body\n ? JSON.stringify(toSnakeCaseKeys(options.body))\n : undefined,\n credentials: \"include\",\n signal: controller.signal,\n },\n meta: {},\n };\n\n await client.runBeforeFetch(ctx);\n\n let res = await fetch(ctx.url, ctx.init);\n\n const action = await client.runAfterFetch(ctx, res);\n if (action === \"retry\" && !ctx.meta.retry) {\n ctx.meta.retry = true;\n res = await fetch(ctx.url, ctx.init);\n }\n\n const setCookieHeaders =\n res.headers.getSetCookie?.() ??\n res.headers.get(\"set-cookie\")?.split(\", \") ??\n null;\n\n if (!res.ok) {\n throw new Error(await res.text());\n }\n\n const resData = await res.json();\n\n // Apply Set-Cookie back into Next.js store (SSR only)\n if (cookieStore && setCookieHeaders) {\n for (const raw of setCookieHeaders) {\n const [pair, ...attrs] = raw.split(\";\");\n const [name, value] = pair.split(\"=\");\n\n if (!name || !value) continue;\n\n const options: Record<string, any> = {};\n\n for (const attr of attrs) {\n const [k, v] = attr.trim().split(\"=\");\n\n switch (k.toLowerCase()) {\n case \"path\":\n options.path = v;\n break;\n case \"expires\":\n options.expires = new Date(v);\n break;\n case \"max-age\":\n options.maxAge = Number(v);\n break;\n case \"httponly\":\n options.httpOnly = true;\n break;\n case \"secure\":\n options.secure = true;\n break;\n case \"samesite\":\n options.sameSite = v?.toLowerCase();\n break;\n }\n }\n\n cookieStore.set(name.trim(), decodeURIComponent(value.trim()), options);\n }\n }\n\n return toCamelCaseKeys(resData) as T;\n}\n"],"mappings":";;;;AAEA,MAAa,aAAaA,MAAE,OAAO;CACjC,IAAIA,MAAE,KAAK;CACX,MAAMA,MAAE,OAAO,CAAC,CAAC,SAAS;CAC1B,OAAOA,MAAE,MAAM;CACf,eAAeA,MAAE,QAAQ;CACzB,OAAOA,MAAE,OAAO,CAAC,CAAC,QAAQ;CAC1B,UAAUA,MAAE,OAAOA,MAAE,OAAO,GAAGA,MAAE,QAAQ,CAAC,CAAC,CAAC,QAAQ;CACpD,WAAWA,MAAE,IAAI,SAAS;CAC1B,WAAWA,MAAE,IAAI,SAAS;AAC5B,CAAC;AAGD,MAAa,gBAAgBA,MAAE,OAAO;CACpC,IAAIA,MAAE,KAAK;CACX,QAAQA,MAAE,OAAO,CAAC,CAAC,SAAS;CAC5B,WAAWA,MAAE,OAAO,CAAC,CAAC,SAAS;CAC/B,YAAYA,MAAE,OAAO,CAAC,CAAC,SAAS;CAChC,aAAaA,MAAE,OAAO,CAAC,CAAC,QAAQ;CAChC,cAAcA,MAAE,OAAO,CAAC,CAAC,QAAQ;CACjC,SAASA,MAAE,OAAO,CAAC,CAAC,QAAQ;CAC5B,sBAAsBA,MAAE,IAAI,SAAS,CAAC,CAAC,QAA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,GAAG,KAAK,KAAK,KAAK,CAAC,CAAC,MAAM,GAAG;GAEpC,QAAQ,EAAE,YAAY,GAAtB;IACE,KAAK;KACH,QAAQ,OAAO;KACf;IACF,KAAK;KACH,QAAQ,UAAU,IAAI,KAAK,CAAC;KAC5B;IACF,KAAK;KACH,QAAQ,SAAS,OAAO,CAAC;KACzB;IACF,KAAK;KACH,QAAQ,WAAW;KACnB;IACF,KAAK;KACH,QAAQ,SAAS;KACjB;IACF,KAAK;KACH,QAAQ,WAAW,GAAG,YAAY;KAClC;GACJ;EACF;EAEA,YAAY,IAAI,KAAK,KAAK,GAAG,mBAAmB,MAAM,KAAK,CAAC,GAAG,OAAO;CACxE;CAGF,uCAAuB,OAAO;AAChC"}
package/dist/index.cjs DELETED
@@ -1,60 +0,0 @@
1
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
- const require_fetch = require('./fetch-CEj1gBkL.cjs');
3
- let es_toolkit = require("es-toolkit");
4
-
5
- //#region src/client.ts
6
- var AuthulaClient = class {
7
- config;
8
- plugins;
9
- beforeFetchHooks = [];
10
- afterFetchHooks = [];
11
- constructor(options) {
12
- this.plugins = options.plugins;
13
- const { plugins, ...rest } = options;
14
- this.config = rest;
15
- for (const plugin of this.plugins) {
16
- if ("attachCookies" in plugin && this.config.cookies) plugin.attachCookies(this.config.cookies);
17
- this[plugin.id] = plugin.init(this);
18
- }
19
- }
20
- registerBeforeFetch(hook) {
21
- this.beforeFetchHooks.push(hook);
22
- }
23
- registerAfterFetch(hook) {
24
- this.afterFetchHooks.push(hook);
25
- }
26
- async runBeforeFetch(ctx) {
27
- for (const hook of this.beforeFetchHooks) await hook(ctx);
28
- }
29
- async runAfterFetch(ctx, res) {
30
- for (const hook of this.afterFetchHooks) if (await hook(ctx, res) === "retry") return "retry";
31
- }
32
- async getMe() {
33
- return require_fetch.wrappedFetch(this, "/me", { method: "GET" });
34
- }
35
- async signOut(data) {
36
- return require_fetch.wrappedFetch(this, "/sign-out", {
37
- method: "POST",
38
- body: (0, es_toolkit.toSnakeCaseKeys)(data)
39
- });
40
- }
41
- getPlugin(id) {
42
- return this.plugins.find((plugin) => plugin.id === id);
43
- }
44
- };
45
-
46
- //#endregion
47
- //#region src/sdk.ts
48
- function createClient(options) {
49
- return new AuthulaClient(options);
50
- }
51
-
52
- //#endregion
53
- exports.AuthulaClient = AuthulaClient;
54
- exports.accountSchema = require_fetch.accountSchema;
55
- exports.createClient = createClient;
56
- exports.sessionSchema = require_fetch.sessionSchema;
57
- exports.userSchema = require_fetch.userSchema;
58
- exports.verificationSchema = require_fetch.verificationSchema;
59
- exports.verificationTypeSchema = require_fetch.verificationTypeSchema;
60
- //# sourceMappingURL=index.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.cjs","names":["wrappedFetch"],"sources":["../src/client.ts","../src/sdk.ts"],"sourcesContent":["import { toSnakeCaseKeys } from \"es-toolkit\";\n\nimport { wrappedFetch } from \"./fetch\";\nimport type {\n FetchContext,\n AuthulaClientConfig,\n AuthulaClientOptions,\n SignOutRequest,\n SignOutResponse,\n Plugin,\n BeforeFetchHook,\n AfterFetchHook,\n} from \"./types\";\n\nexport class AuthulaClient {\n public readonly config: AuthulaClientConfig;\n private readonly plugins: Array<Plugin>;\n private readonly beforeFetchHooks: BeforeFetchHook[] = [];\n private readonly afterFetchHooks: AfterFetchHook[] = [];\n\n constructor(options: AuthulaClientOptions) {\n this.plugins = options.plugins;\n\n const { plugins, ...rest } = options;\n this.config = rest;\n\n for (const plugin of this.plugins) {\n if (\"attachCookies\" in plugin && this.config.cookies) {\n (plugin as any).attachCookies(this.config.cookies);\n }\n (this as any)[plugin.id] = plugin.init(this);\n }\n }\n\n public registerBeforeFetch(hook: BeforeFetchHook): void {\n this.beforeFetchHooks.push(hook);\n }\n\n public registerAfterFetch(hook: AfterFetchHook): void {\n this.afterFetchHooks.push(hook);\n }\n\n public async runBeforeFetch(ctx: FetchContext): Promise<void> {\n for (const hook of this.beforeFetchHooks) {\n await hook(ctx);\n }\n }\n\n public async runAfterFetch(ctx: FetchContext, res: Response) {\n for (const hook of this.afterFetchHooks) {\n const result = await hook(ctx, res);\n if (result === \"retry\") {\n return \"retry\";\n }\n }\n }\n\n public async getMe<T = unknown>(): Promise<T> {\n return wrappedFetch<T>(this, \"/me\", {\n method: \"GET\",\n });\n }\n\n public async signOut(data: SignOutRequest): Promise<SignOutResponse> {\n return wrappedFetch<SignOutResponse>(this, \"/sign-out\", {\n method: \"POST\",\n body: toSnakeCaseKeys(data),\n });\n }\n\n public getPlugin<T extends Plugin>(id: string): T | undefined {\n return this.plugins.find((plugin) => plugin.id === id) as T | undefined;\n }\n}\n","import { AuthulaClient } from \"./client\";\nimport type { AuthulaClientOptions, Plugin } from \"./types\";\n\n// Helper to extract plugin methods\nexport type InferPluginMethods<P> = P extends { init: (client: any) => infer M }\n ? M\n : never;\n\nexport type ClientWithPlugins<T extends readonly Plugin[]> = AuthulaClient & {\n [P in T[number] as P[\"id\"]]: InferPluginMethods<P>;\n};\n\nexport function createClient<const T extends readonly Plugin[]>(\n options: Omit<AuthulaClientOptions, \"plugins\"> & { plugins: T },\n): ClientWithPlugins<T> {\n return new AuthulaClient(options as any) as ClientWithPlugins<T>;\n}\n"],"mappings":";;;;;AAcA,IAAa,gBAAb,MAA2B;CACzB,AAAgB;CAChB,AAAiB;CACjB,AAAiB,mBAAsC,CAAC;CACxD,AAAiB,kBAAoC,CAAC;CAEtD,YAAY,SAA+B;EACzC,KAAK,UAAU,QAAQ;EAEvB,MAAM,EAAE,SAAS,GAAG,SAAS;EAC7B,KAAK,SAAS;EAEd,KAAK,MAAM,UAAU,KAAK,SAAS;GACjC,IAAI,mBAAmB,UAAU,KAAK,OAAO,SAC3C,AAAC,OAAe,cAAc,KAAK,OAAO,OAAO;GAEnD,AAAC,KAAa,OAAO,MAAM,OAAO,KAAK,IAAI;EAC7C;CACF;CAEA,AAAO,oBAAoB,MAA6B;EACtD,KAAK,iBAAiB,KAAK,IAAI;CACjC;CAEA,AAAO,mBAAmB,MAA4B;EACpD,KAAK,gBAAgB,KAAK,IAAI;CAChC;CAEA,MAAa,eAAe,KAAkC;EAC5D,KAAK,MAAM,QAAQ,KAAK,kBACtB,MAAM,KAAK,GAAG;CAElB;CAEA,MAAa,cAAc,KAAmB,KAAe;EAC3D,KAAK,MAAM,QAAQ,KAAK,iBAEtB,IAAI,MADiB,KAAK,KAAK,GAAG,MACnB,SACb,OAAO;CAGb;CAEA,MAAa,QAAiC;EAC5C,OAAOA,2BAAgB,MAAM,OAAO,EAClC,QAAQ,MACV,CAAC;CACH;CAEA,MAAa,QAAQ,MAAgD;EACnE,OAAOA,2BAA8B,MAAM,aAAa;GACtD,QAAQ;GACR,sCAAsB,IAAI;EAC5B,CAAC;CACH;CAEA,AAAO,UAA4B,IAA2B;EAC5D,OAAO,KAAK,QAAQ,MAAM,WAAW,OAAO,OAAO,EAAE;CACvD;AACF;;;;AC7DA,SAAgB,aACd,SACsB;CACtB,OAAO,IAAI,cAAc,OAAc;AACzC"}
package/dist/index.d.cts DELETED
@@ -1,2 +0,0 @@
1
- import { C as AfterFetchHook, E as FetchRequestOptions, S as verificationTypeSchema, T as FetchContext, _ as VerificationType, a as SignOutResponse, b as userSchema, c as FetchOptions, d as CookieProvider, f as CookieStore, g as Verification, h as User, i as SignOutRequest, l as Plugin, m as Session, n as createClient, o as AuthulaClientConfig, p as Account, r as GetMeResponse, s as AuthulaClientOptions, t as ClientWithPlugins, u as AuthulaClient, v as accountSchema, w as BeforeFetchHook, x as verificationSchema, y as sessionSchema } from "./sdk-7eSlhCrf.cjs";
2
- export { Account, AfterFetchHook, AuthulaClient, AuthulaClientConfig, AuthulaClientOptions, BeforeFetchHook, type ClientWithPlugins, CookieProvider, CookieStore, FetchContext, FetchOptions, FetchRequestOptions, GetMeResponse, Plugin, Session, SignOutRequest, SignOutResponse, User, Verification, VerificationType, accountSchema, createClient, sessionSchema, userSchema, verificationSchema, verificationTypeSchema };