prostgles-client 4.0.422 → 4.0.425

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.
@@ -1,6 +1,9 @@
1
- import type { AuthRequest, AuthResponse, AuthSocketSchema, IdentityProvider, LocalLoginMode, UserLike } from "prostgles-types";
1
+ import type { AuthRequest, AuthSocketSchema, IdentityProvider, LocalLoginMode, UserLike } from "prostgles-types";
2
2
  import { type InitOptions } from "./prostgles";
3
3
  import type { Socket } from "socket.io-client";
4
+ import type { ClientAuthResponse } from "./auth/authRequest";
5
+ import { type ClientAuthCalls } from "./auth/getAuthCalls";
6
+ export { authRequest } from "./auth/authRequest";
4
7
  type OptionalToUndefined<T> = {
5
8
  [K in {} & keyof T]: T[K];
6
9
  };
@@ -11,22 +14,17 @@ type Args = Pick<OptionalToUndefined<InitOptions>, "credentials" | "redirect"> &
11
14
  endpoint: string | undefined;
12
15
  };
13
16
  type WithProviderLogin = Partial<Record<IdentityProvider, VoidFunction>>;
14
- type ClientAuthSuccess<T> = T & {
15
- /**
16
- * This is a client-only property that is obtained from server redirect response
17
- */
18
- redirect_url?: string;
19
- };
20
- export type PasswordLoginResponse = ClientAuthSuccess<AuthResponse.PasswordLoginFailure | AuthResponse.PasswordLoginSuccess | AuthResponse.MagicLinkAuthSuccess | AuthResponse.MagicLinkAuthFailure | AuthResponse.OAuthRegisterSuccess | AuthResponse.OAuthRegisterFailure>;
21
- export type PasswordRegisterResponse = ClientAuthSuccess<AuthResponse.PasswordRegisterSuccess | AuthResponse.PasswordRegisterFailure>;
22
- export type PasswordRegister = (params: AuthRequest.RegisterData) => Promise<PasswordRegisterResponse>;
23
- export type PasswordLogin = (params: AuthRequest.LoginData) => Promise<PasswordLoginResponse>;
17
+ export type PasswordLoginResponse = ClientAuthResponse<"login">;
18
+ export type PasswordRegisterResponse = ClientAuthResponse<"register">;
19
+ export type PasswordRegister = ClientAuthCalls["register"];
20
+ export type PasswordLogin = ClientAuthCalls["login"];
21
+ export type ConfirmEmail = (data: AuthRequest.ConfirmEmailData) => Promise<ClientAuthResponse<"magicLinkOrOTP">>;
24
22
  type LoginSignupOptions = Pick<AuthSocketSchema, "providers" | "preferredLogin"> & {
25
23
  loginWithProvider: undefined | WithProviderLogin;
26
24
  login: undefined | PasswordLogin;
27
25
  loginType: LocalLoginMode | undefined;
28
26
  signupWithEmailAndPassword: undefined | PasswordRegister;
29
- confirmEmail: undefined | ((data: AuthRequest.ConfirmEmailData) => Promise<PasswordRegisterResponse>);
27
+ confirmEmail: undefined | ConfirmEmail;
30
28
  };
31
29
  type AuthStateLoggedOut = LoginSignupOptions & {
32
30
  isLoggedin: false;
@@ -35,10 +33,8 @@ type AuthStateLoggedOut = LoginSignupOptions & {
35
33
  type AuthStateLoggedIn<U extends UserLike = UserLike> = LoginSignupOptions & {
36
34
  isLoggedin: true;
37
35
  user: U;
38
- logout: () => Promise<any>;
36
+ logout: () => Promise<ClientAuthResponse<"logout">>;
39
37
  };
40
38
  export type AuthHandler<U extends UserLike = UserLike> = AuthStateLoggedOut | AuthStateLoggedIn<U>;
41
39
  export declare const getAuthHandler: ({ authData: authConfig, socket, onReload, endpoint, ...authOpts }: Args) => AuthHandler;
42
- export declare const authRequest: <T extends PasswordRegisterResponse | PasswordLoginResponse>(path: string, data: object, method: "GET" | "POST", { credentials, redirect }: Pick<Partial<Request>, "credentials" | "redirect">) => Promise<T>;
43
- export {};
44
40
  //# sourceMappingURL=getAuthHandler.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"getAuthHandler.d.ts","sourceRoot":"","sources":["../lib/getAuthHandler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAGV,WAAW,EACX,YAAY,EACZ,gBAAgB,EAChB,gBAAgB,EAChB,cAAc,EACd,QAAQ,EACT,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EAAgB,KAAK,WAAW,EAAE,MAAM,aAAa,CAAC;AAC7D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM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AAE,WAAW,GAAG,gBAAgB,CAAC,GAAG;IACjF,iBAAiB,EAAE,SAAS,GAAG,iBAAiB,CAAC;IACjD,KAAK,EAAE,SAAS,GAAG,aAAa,CAAC;IACjC,SAAS,EAAE,cAAc,GAAG,SAAS,CAAC;IACtC,0BAA0B,EAAE,SAAS,GAAG,gBAAgB,CAAC;IACzD,YAAY,EAAE,SAAS,GAAG,YAAY,CAAC;CACxC,CAAC;AAEF,KAAK,kBAAkB,GAAG,kBAAkB,GAAG;IAC7C,UAAU,EAAE,KAAK,CAAC;IAClB,IAAI,CAAC,EAAE,SAAS,CAAC;CAClB,CAAC;AAEF,KAAK,iBAAiB,CAAC,CAAC,SAAS,QAAQ,GAAG,QAAQ,IAAI,kBAAkB,GAAG;IAC3E,UAAU,EAAE,IAAI,CAAC;IACjB,IAAI,EAAE,CAAC,CAAC;IACR,MAAM,EAAE,MAAM,OAAO,CAAC,kBAAkB,CAAC,QAAQ,CAAC,CAAC,CAAC;CACrD,CAAC;AAEF,MAAM,MAAM,WAAW,CAAC,CAAC,SAAS,QAAQ,GAAG,QAAQ,IAAI,kBAAkB,GAAG,iBAAiB,CAAC,CAAC,CAAC,CAAC;AAEnG,eAAO,MAAM,cAAc,GAAI,mEAM5B,IAAI,KAAG,WAsFT,CAAC"}
@@ -1,8 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.authRequest = exports.getAuthHandler = void 0;
3
+ exports.getAuthHandler = exports.authRequest = void 0;
4
4
  const prostgles_types_1 = require("prostgles-types");
5
5
  const prostgles_1 = require("./prostgles");
6
+ const getAuthCalls_1 = require("./auth/getAuthCalls");
7
+ var authRequest_1 = require("./auth/authRequest");
8
+ Object.defineProperty(exports, "authRequest", { enumerable: true, get: function () { return authRequest_1.authRequest; } });
6
9
  const getAuthHandler = ({ authData: authConfig, socket, onReload, endpoint, ...authOpts }) => {
7
10
  const urlWithEndpointAndSearch = (route) => {
8
11
  const search = typeof window !== "undefined" ? window.location.search : "";
@@ -11,6 +14,12 @@ const getAuthHandler = ({ authData: authConfig, socket, onReload, endpoint, ...a
11
14
  url = `${endpoint}${url}`;
12
15
  return url;
13
16
  };
17
+ const authCalls = (0, getAuthCalls_1.getAuthCalls)({
18
+ login: authConfig?.login?.loginRoute,
19
+ register: authConfig?.signupWithEmailAndPassword?.url,
20
+ magicLinkOrOTP: authConfig?.signupWithEmailAndPassword?.emailConfirmationRoute,
21
+ logout: authConfig?.login?.logoutRoute,
22
+ }, urlWithEndpointAndSearch, authOpts);
14
23
  if (authConfig?.pathGuard && prostgles_1.isClientSide) {
15
24
  const doReload = (res) => {
16
25
  if (res?.shouldReload) {
@@ -51,21 +60,10 @@ const getAuthHandler = ({ authData: authConfig, socket, onReload, endpoint, ...a
51
60
  };
52
61
  return acc;
53
62
  }, {}));
54
- loginSignupOptions.login =
55
- login &&
56
- (async (params) => {
57
- return (0, exports.authRequest)(urlWithEndpointAndSearch(login.loginRoute), params, "POST", authOpts);
58
- });
63
+ loginSignupOptions.login = login && authCalls.login;
59
64
  loginSignupOptions.signupWithEmailAndPassword =
60
- signupWithEmailAndPassword &&
61
- ((params) => {
62
- return (0, exports.authRequest)(urlWithEndpointAndSearch(signupWithEmailAndPassword.url), params, "POST", authOpts);
63
- });
64
- loginSignupOptions.confirmEmail =
65
- signupWithEmailAndPassword &&
66
- ((data) => {
67
- return (0, exports.authRequest)(urlWithEndpointAndSearch(signupWithEmailAndPassword.emailConfirmationRoute), data, "POST", authOpts);
68
- });
65
+ signupWithEmailAndPassword && authCalls.register;
66
+ loginSignupOptions.confirmEmail = signupWithEmailAndPassword && authCalls.magicLinkOrOTP;
69
67
  }
70
68
  if (!authConfig?.user) {
71
69
  return {
@@ -77,44 +75,8 @@ const getAuthHandler = ({ authData: authConfig, socket, onReload, endpoint, ...a
77
75
  return {
78
76
  isLoggedin: true,
79
77
  user: authConfig.user,
80
- logout: async () => {
81
- const { logoutRoute } = authConfig.login ?? {};
82
- if (!logoutRoute)
83
- throw new Error("Unexpected");
84
- return (0, exports.authRequest)(urlWithEndpointAndSearch(logoutRoute), {}, "POST", authOpts);
85
- },
78
+ logout: () => authCalls.logout({}),
86
79
  ...loginSignupOptions,
87
80
  };
88
81
  };
89
82
  exports.getAuthHandler = getAuthHandler;
90
- const authRequest = async (path, data, method, { credentials, redirect }) => {
91
- const rawResponse = await fetch(path, {
92
- method: method,
93
- headers: {
94
- Accept: "application/json",
95
- "Content-Type": "application/json",
96
- },
97
- ...(method !== "GET" && { body: JSON.stringify(data) }),
98
- credentials,
99
- redirect,
100
- });
101
- if (!rawResponse.ok) {
102
- const error = await rawResponse
103
- .json()
104
- .catch(() => rawResponse.text())
105
- .catch(() => rawResponse.statusText);
106
- if (typeof error === "string") {
107
- return { success: false, code: "something-went-wrong", message: error };
108
- }
109
- return error;
110
- }
111
- const responseObject = await rawResponse
112
- .json()
113
- .catch(async () => ({ message: await rawResponse.text() }))
114
- .catch(() => ({ message: rawResponse.statusText }));
115
- if (rawResponse.redirected) {
116
- return { ...responseObject, success: true, redirect_url: rawResponse.url };
117
- }
118
- return responseObject;
119
- };
120
- exports.authRequest = authRequest;