@yackey-labs/yauth-client 0.1.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.
Files changed (55) hide show
  1. package/dist/index.js +196 -0
  2. package/package.json +30 -0
  3. package/src/bindings/AccountLockoutMessageResponse.ts +3 -0
  4. package/src/bindings/ApiKeyResponse.ts +3 -0
  5. package/src/bindings/AuthConfigResponse.ts +14 -0
  6. package/src/bindings/AuthMethod.ts +3 -0
  7. package/src/bindings/AuthUser.ts +4 -0
  8. package/src/bindings/AuthorizeQuery.ts +3 -0
  9. package/src/bindings/AuthorizeResponse.ts +3 -0
  10. package/src/bindings/BackupCodeCountResponse.ts +3 -0
  11. package/src/bindings/BackupCodesResponse.ts +3 -0
  12. package/src/bindings/BanRequest.ts +3 -0
  13. package/src/bindings/CallbackBody.ts +3 -0
  14. package/src/bindings/ChangePasswordRequest.ts +3 -0
  15. package/src/bindings/ConfirmTotpRequest.ts +3 -0
  16. package/src/bindings/CreateApiKeyRequest.ts +3 -0
  17. package/src/bindings/CreateApiKeyResponse.ts +3 -0
  18. package/src/bindings/CreateWebhookRequest.ts +3 -0
  19. package/src/bindings/ForgotPasswordRequest.ts +3 -0
  20. package/src/bindings/ListSessionsQuery.ts +3 -0
  21. package/src/bindings/ListUsersQuery.ts +3 -0
  22. package/src/bindings/LoginRequest.ts +3 -0
  23. package/src/bindings/MagicLinkMessageResponse.ts +3 -0
  24. package/src/bindings/MagicLinkSendRequest.ts +3 -0
  25. package/src/bindings/MagicLinkVerifyRequest.ts +3 -0
  26. package/src/bindings/MessageResponse.ts +3 -0
  27. package/src/bindings/MfaAuthResponse.ts +3 -0
  28. package/src/bindings/MfaMessageResponse.ts +3 -0
  29. package/src/bindings/OAuthAccountResponse.ts +3 -0
  30. package/src/bindings/OAuthAuthResponse.ts +3 -0
  31. package/src/bindings/PasskeyInfo.ts +3 -0
  32. package/src/bindings/PasskeyLoginBeginRequest.ts +3 -0
  33. package/src/bindings/PasskeyLoginFinishRequest.ts +3 -0
  34. package/src/bindings/RefreshRequest.ts +3 -0
  35. package/src/bindings/RegisterFinishRequest.ts +3 -0
  36. package/src/bindings/RegisterRequest.ts +3 -0
  37. package/src/bindings/RequestUnlockRequest.ts +3 -0
  38. package/src/bindings/ResendVerificationRequest.ts +3 -0
  39. package/src/bindings/ResetPasswordRequest.ts +3 -0
  40. package/src/bindings/RevokeRequest.ts +3 -0
  41. package/src/bindings/SetupTotpResponse.ts +3 -0
  42. package/src/bindings/TokenRequest.ts +7 -0
  43. package/src/bindings/TokenResponse.ts +3 -0
  44. package/src/bindings/UnlockAccountRequest.ts +3 -0
  45. package/src/bindings/UpdateProfileRequest.ts +3 -0
  46. package/src/bindings/UpdateUserRequest.ts +3 -0
  47. package/src/bindings/UpdateWebhookRequest.ts +3 -0
  48. package/src/bindings/VerifyEmailRequest.ts +3 -0
  49. package/src/bindings/VerifyMfaRequest.ts +3 -0
  50. package/src/bindings/WebhookDeliveryResponse.ts +3 -0
  51. package/src/bindings/WebhookDetailResponse.ts +5 -0
  52. package/src/bindings/WebhookResponse.ts +3 -0
  53. package/src/generated.test.ts +337 -0
  54. package/src/generated.ts +385 -0
  55. package/src/index.ts +419 -0
@@ -0,0 +1,385 @@
1
+ // Auto-generated by axum-ts-client. Do not edit.
2
+
3
+ import type { AccountLockoutMessageResponse } from "./bindings/AccountLockoutMessageResponse";
4
+ import type { ApiKeyResponse } from "./bindings/ApiKeyResponse";
5
+ import type { AuthConfigResponse } from "./bindings/AuthConfigResponse";
6
+ import type { AuthorizeQuery } from "./bindings/AuthorizeQuery";
7
+ import type { AuthorizeResponse } from "./bindings/AuthorizeResponse";
8
+ import type { AuthUser } from "./bindings/AuthUser";
9
+ import type { BackupCodeCountResponse } from "./bindings/BackupCodeCountResponse";
10
+ import type { BackupCodesResponse } from "./bindings/BackupCodesResponse";
11
+ import type { BanRequest } from "./bindings/BanRequest";
12
+ import type { CallbackBody } from "./bindings/CallbackBody";
13
+ import type { ChangePasswordRequest } from "./bindings/ChangePasswordRequest";
14
+ import type { ConfirmTotpRequest } from "./bindings/ConfirmTotpRequest";
15
+ import type { CreateApiKeyRequest } from "./bindings/CreateApiKeyRequest";
16
+ import type { CreateApiKeyResponse } from "./bindings/CreateApiKeyResponse";
17
+ import type { CreateWebhookRequest } from "./bindings/CreateWebhookRequest";
18
+ import type { ForgotPasswordRequest } from "./bindings/ForgotPasswordRequest";
19
+ import type { ListSessionsQuery } from "./bindings/ListSessionsQuery";
20
+ import type { ListUsersQuery } from "./bindings/ListUsersQuery";
21
+ import type { LoginRequest } from "./bindings/LoginRequest";
22
+ import type { MagicLinkMessageResponse } from "./bindings/MagicLinkMessageResponse";
23
+ import type { MagicLinkSendRequest } from "./bindings/MagicLinkSendRequest";
24
+ import type { MagicLinkVerifyRequest } from "./bindings/MagicLinkVerifyRequest";
25
+ import type { MessageResponse } from "./bindings/MessageResponse";
26
+ import type { MfaAuthResponse } from "./bindings/MfaAuthResponse";
27
+ import type { MfaMessageResponse } from "./bindings/MfaMessageResponse";
28
+ import type { OAuthAccountResponse } from "./bindings/OAuthAccountResponse";
29
+ import type { OAuthAuthResponse } from "./bindings/OAuthAuthResponse";
30
+ import type { PasskeyInfo } from "./bindings/PasskeyInfo";
31
+ import type { PasskeyLoginBeginRequest } from "./bindings/PasskeyLoginBeginRequest";
32
+ import type { PasskeyLoginFinishRequest } from "./bindings/PasskeyLoginFinishRequest";
33
+ import type { RefreshRequest } from "./bindings/RefreshRequest";
34
+ import type { RegisterFinishRequest } from "./bindings/RegisterFinishRequest";
35
+ import type { RegisterRequest } from "./bindings/RegisterRequest";
36
+ import type { RequestUnlockRequest } from "./bindings/RequestUnlockRequest";
37
+ import type { ResendVerificationRequest } from "./bindings/ResendVerificationRequest";
38
+ import type { ResetPasswordRequest } from "./bindings/ResetPasswordRequest";
39
+ import type { RevokeRequest } from "./bindings/RevokeRequest";
40
+ import type { SetupTotpResponse } from "./bindings/SetupTotpResponse";
41
+ import type { TokenRequest } from "./bindings/TokenRequest";
42
+ import type { TokenResponse } from "./bindings/TokenResponse";
43
+ import type { UnlockAccountRequest } from "./bindings/UnlockAccountRequest";
44
+ import type { UpdateProfileRequest } from "./bindings/UpdateProfileRequest";
45
+ import type { UpdateUserRequest } from "./bindings/UpdateUserRequest";
46
+ import type { UpdateWebhookRequest } from "./bindings/UpdateWebhookRequest";
47
+ import type { VerifyEmailRequest } from "./bindings/VerifyEmailRequest";
48
+ import type { VerifyMfaRequest } from "./bindings/VerifyMfaRequest";
49
+ import type { WebhookDetailResponse } from "./bindings/WebhookDetailResponse";
50
+ import type { WebhookResponse } from "./bindings/WebhookResponse";
51
+
52
+ export class YAuthError extends Error {
53
+ constructor(
54
+ message: string,
55
+ public status: number,
56
+ public body?: unknown,
57
+ ) {
58
+ super(message);
59
+ this.name = "YAuthError";
60
+ }
61
+ }
62
+
63
+ export interface YAuthClientOptions {
64
+ baseUrl: string;
65
+ getToken?: () => Promise<string | null>;
66
+ credentials?: RequestCredentials;
67
+ fetch?: typeof fetch;
68
+ onError?: (error: YAuthError) => void;
69
+ }
70
+
71
+ type RequestOptions = {
72
+ method?: string;
73
+ body?: unknown;
74
+ query?: Record<string, unknown>;
75
+ auth?: boolean;
76
+ };
77
+
78
+ function createRequest(options: YAuthClientOptions) {
79
+ const { baseUrl, credentials = "include" } = options;
80
+ const fetchFn = options.fetch ?? globalThis.fetch;
81
+
82
+ async function request<T>(
83
+ path: string,
84
+ opts: RequestOptions = {},
85
+ ): Promise<T> {
86
+ const { method = "GET", body, query, auth } = opts;
87
+
88
+ let url = `${baseUrl}${path}`;
89
+ if (query) {
90
+ const params = new URLSearchParams();
91
+ for (const [key, value] of Object.entries(query)) {
92
+ if (value !== undefined && value !== null) {
93
+ params.set(key, String(value));
94
+ }
95
+ }
96
+ const qs = params.toString();
97
+ if (qs) url += `?${qs}`;
98
+ }
99
+
100
+ const headers: Record<string, string> = {
101
+ "Content-Type": "application/json",
102
+ };
103
+
104
+ if (auth && options.getToken) {
105
+ const token = await options.getToken();
106
+ if (token) headers.Authorization = `Bearer ${token}`;
107
+ }
108
+
109
+ const response = await fetchFn(url, {
110
+ method,
111
+ credentials,
112
+ headers,
113
+ body: body ? JSON.stringify(body) : undefined,
114
+ });
115
+
116
+ if (!response.ok) {
117
+ const text = await response.text();
118
+ let message: string;
119
+ let errorBody: unknown;
120
+ try {
121
+ const json = JSON.parse(text);
122
+ message = json.error ?? json.message ?? text;
123
+ errorBody = json;
124
+ } catch {
125
+ message = text;
126
+ }
127
+ const error = new YAuthError(message, response.status, errorBody);
128
+ if (options.onError) options.onError(error);
129
+ throw error;
130
+ }
131
+
132
+ const text = await response.text();
133
+ return (text ? JSON.parse(text) : undefined) as T;
134
+ }
135
+
136
+ return request;
137
+ }
138
+
139
+ export function createYAuthClient(options: YAuthClientOptions) {
140
+ const request = createRequest(options);
141
+
142
+ return {
143
+ getConfig: () => request<AuthConfigResponse>("/config"),
144
+ getSession: () => request<AuthUser>("/session", { auth: true }),
145
+ logout: () => request<void>("/logout", { method: "POST", auth: true }),
146
+ updateProfile: (body: UpdateProfileRequest) =>
147
+ request<AuthUser>("/me", { method: "PATCH", auth: true, body }),
148
+
149
+ accountLockout: {
150
+ requestUnlock: (body: RequestUnlockRequest) =>
151
+ request<AccountLockoutMessageResponse>("/account/request-unlock", {
152
+ method: "POST",
153
+ body,
154
+ }),
155
+ unlock: (body: UnlockAccountRequest) =>
156
+ request<AccountLockoutMessageResponse>("/account/unlock", {
157
+ method: "POST",
158
+ body,
159
+ }),
160
+ adminUnlock: (id: string) =>
161
+ request<AccountLockoutMessageResponse>(`/admin/users/${id}/unlock`, {
162
+ method: "POST",
163
+ auth: true,
164
+ }),
165
+ },
166
+
167
+ admin: {
168
+ listUsers: (query?: ListUsersQuery) =>
169
+ request<void>("/admin/users", { auth: true, query }),
170
+ getUser: (id: string) =>
171
+ request<AuthUser>(`/admin/users/${id}`, { auth: true }),
172
+ updateUser: (id: string, body: UpdateUserRequest) =>
173
+ request<AuthUser>(`/admin/users/${id}`, {
174
+ method: "PUT",
175
+ auth: true,
176
+ body,
177
+ }),
178
+ deleteUser: (id: string) =>
179
+ request<void>(`/admin/users/${id}`, { method: "DELETE", auth: true }),
180
+ banUser: (id: string, body: BanRequest) =>
181
+ request<AuthUser>(`/admin/users/${id}/ban`, {
182
+ method: "POST",
183
+ auth: true,
184
+ body,
185
+ }),
186
+ unbanUser: (id: string) =>
187
+ request<AuthUser>(`/admin/users/${id}/unban`, {
188
+ method: "POST",
189
+ auth: true,
190
+ }),
191
+ impersonate: (id: string) =>
192
+ request<void>(`/admin/users/${id}/impersonate`, {
193
+ method: "POST",
194
+ auth: true,
195
+ }),
196
+ listSessions: (query?: ListSessionsQuery) =>
197
+ request<void>("/admin/sessions", { auth: true, query }),
198
+ deleteSession: (id: string) =>
199
+ request<void>(`/admin/sessions/${id}`, {
200
+ method: "DELETE",
201
+ auth: true,
202
+ }),
203
+ },
204
+
205
+ apiKeys: {
206
+ create: (body: CreateApiKeyRequest) =>
207
+ request<CreateApiKeyResponse>("/api-keys", {
208
+ method: "POST",
209
+ auth: true,
210
+ body,
211
+ }),
212
+ list: () => request<ApiKeyResponse>("/api-keys", { auth: true }),
213
+ delete: (id: string) =>
214
+ request<void>(`/api-keys/${id}`, { method: "DELETE", auth: true }),
215
+ },
216
+
217
+ bearer: {
218
+ getToken: (body: TokenRequest) =>
219
+ request<TokenResponse>("/token", { method: "POST", body }),
220
+ refresh: (body: RefreshRequest) =>
221
+ request<TokenResponse>("/token/refresh", { method: "POST", body }),
222
+ revoke: (body: RevokeRequest) =>
223
+ request<void>("/token/revoke", { method: "POST", auth: true, body }),
224
+ },
225
+
226
+ emailPassword: {
227
+ register: (body: RegisterRequest) =>
228
+ request<MessageResponse>("/register", { method: "POST", body }),
229
+ login: (body: LoginRequest) =>
230
+ request<void>("/login", { method: "POST", body }),
231
+ verifyEmail: (body: VerifyEmailRequest) =>
232
+ request<MessageResponse>("/verify-email", { method: "POST", body }),
233
+ resendVerification: (body: ResendVerificationRequest) =>
234
+ request<MessageResponse>("/resend-verification", {
235
+ method: "POST",
236
+ body,
237
+ }),
238
+ forgotPassword: (body: ForgotPasswordRequest) =>
239
+ request<MessageResponse>("/forgot-password", { method: "POST", body }),
240
+ resetPassword: (body: ResetPasswordRequest) =>
241
+ request<MessageResponse>("/reset-password", { method: "POST", body }),
242
+ changePassword: (body: ChangePasswordRequest) =>
243
+ request<MessageResponse>("/change-password", {
244
+ method: "POST",
245
+ auth: true,
246
+ body,
247
+ }),
248
+ },
249
+
250
+ magicLink: {
251
+ send: (body: MagicLinkSendRequest) =>
252
+ request<MagicLinkMessageResponse>("/magic-link/send", {
253
+ method: "POST",
254
+ body,
255
+ }),
256
+ verify: (body: MagicLinkVerifyRequest) =>
257
+ request<void>("/magic-link/verify", { method: "POST", body }),
258
+ },
259
+
260
+ mfa: {
261
+ setup: () =>
262
+ request<SetupTotpResponse>("/mfa/totp/setup", {
263
+ method: "POST",
264
+ auth: true,
265
+ }),
266
+ confirm: (body: ConfirmTotpRequest) =>
267
+ request<MfaMessageResponse>("/mfa/totp/confirm", {
268
+ method: "POST",
269
+ auth: true,
270
+ body,
271
+ }),
272
+ disable: () =>
273
+ request<MfaMessageResponse>("/mfa/totp", {
274
+ method: "DELETE",
275
+ auth: true,
276
+ }),
277
+ verify: (body: VerifyMfaRequest) =>
278
+ request<MfaAuthResponse>("/mfa/verify", { method: "POST", body }),
279
+ getBackupCodeCount: () =>
280
+ request<BackupCodeCountResponse>("/mfa/backup-codes", { auth: true }),
281
+ regenerateBackupCodes: () =>
282
+ request<BackupCodesResponse>("/mfa/backup-codes/regenerate", {
283
+ method: "POST",
284
+ auth: true,
285
+ }),
286
+ },
287
+
288
+ oauth: {
289
+ authorize: (provider: string, query?: AuthorizeQuery) => {
290
+ let url = `${options.baseUrl}/oauth/${provider}/authorize`;
291
+ if (query) {
292
+ const params = new URLSearchParams();
293
+ for (const [key, value] of Object.entries(query)) {
294
+ if (value !== undefined && value !== null)
295
+ params.set(key, String(value));
296
+ }
297
+ const qs = params.toString();
298
+ if (qs) url += `?${qs}`;
299
+ }
300
+ return url;
301
+ },
302
+ callback: (provider: string, body: CallbackBody) =>
303
+ request<OAuthAuthResponse>(`/oauth/${provider}/callback`, {
304
+ method: "POST",
305
+ body,
306
+ }),
307
+ accounts: () =>
308
+ request<OAuthAccountResponse>("/oauth/accounts", { auth: true }),
309
+ unlink: (provider: string) =>
310
+ request<void>(`/oauth/${provider}`, { method: "DELETE", auth: true }),
311
+ link: (provider: string) =>
312
+ request<AuthorizeResponse>(`/oauth/${provider}/link`, {
313
+ method: "POST",
314
+ auth: true,
315
+ }),
316
+ },
317
+
318
+ oauth2Server: {
319
+ metadata: () => request<void>("/.well-known/oauth-authorization-server"),
320
+ authorize: () => request<void>("/oauth/authorize"),
321
+ authorizeConsent: () =>
322
+ request<void>("/oauth/authorize", { method: "POST" }),
323
+ token: () => request<void>("/oauth/token", { method: "POST" }),
324
+ introspect: () => request<void>("/oauth/introspect", { method: "POST" }),
325
+ revoke: () => request<void>("/oauth/revoke", { method: "POST" }),
326
+ register: () => request<void>("/oauth/register", { method: "POST" }),
327
+ deviceAuthorize: () =>
328
+ request<void>("/oauth/device/code", { method: "POST" }),
329
+ deviceVerify: () => request<void>("/oauth/device"),
330
+ deviceApprove: () => request<void>("/oauth/device", { method: "POST" }),
331
+ },
332
+
333
+ oidc: {
334
+ openidConfiguration: () =>
335
+ request<void>("/.well-known/openid-configuration"),
336
+ jwks: () => request<void>("/.well-known/jwks.json"),
337
+ userinfo: () => request<void>("/userinfo", { auth: true }),
338
+ },
339
+
340
+ passkey: {
341
+ loginBegin: (body: PasskeyLoginBeginRequest) =>
342
+ request<void>("/passkey/login/begin", { method: "POST", body }),
343
+ loginFinish: (body: PasskeyLoginFinishRequest) =>
344
+ request<void>("/passkey/login/finish", { method: "POST", body }),
345
+ registerBegin: () =>
346
+ request<void>("/passkeys/register/begin", {
347
+ method: "POST",
348
+ auth: true,
349
+ }),
350
+ registerFinish: (body: RegisterFinishRequest) =>
351
+ request<void>("/passkeys/register/finish", {
352
+ method: "POST",
353
+ auth: true,
354
+ body,
355
+ }),
356
+ list: () => request<PasskeyInfo>("/passkeys", { auth: true }),
357
+ delete: (id: string) =>
358
+ request<void>(`/passkeys/${id}`, { method: "DELETE", auth: true }),
359
+ },
360
+
361
+ webhooks: {
362
+ create: (body: CreateWebhookRequest) =>
363
+ request<WebhookResponse>("/webhooks", {
364
+ method: "POST",
365
+ auth: true,
366
+ body,
367
+ }),
368
+ list: () => request<WebhookResponse>("/webhooks", { auth: true }),
369
+ get: (id: string) =>
370
+ request<WebhookDetailResponse>(`/webhooks/${id}`, { auth: true }),
371
+ update: (id: string, body: UpdateWebhookRequest) =>
372
+ request<WebhookResponse>(`/webhooks/${id}`, {
373
+ method: "PUT",
374
+ auth: true,
375
+ body,
376
+ }),
377
+ delete: (id: string) =>
378
+ request<void>(`/webhooks/${id}`, { method: "DELETE", auth: true }),
379
+ test: (id: string) =>
380
+ request<void>(`/webhooks/${id}/test`, { method: "POST", auth: true }),
381
+ },
382
+ };
383
+ }
384
+
385
+ export type YAuthClient = ReturnType<typeof createYAuthClient>;