Compare commits
2 Commits
638e198d02
...
55451d3eb4
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
55451d3eb4 | ||
|
|
aa77f4b311 |
@@ -1,9 +1,66 @@
|
||||
import { create } from "zustand";
|
||||
import type { AuthStore } from "../../types/store";
|
||||
import { login, logout, register } from "../../../api";
|
||||
import { callApi } from "shared/utils";
|
||||
|
||||
export const useAuthStore = create<AuthStore>()((set) => ({
|
||||
user: undefined,
|
||||
status: "idle",
|
||||
setUser: (user) => set({ user }),
|
||||
setStatus: (status) => set({ status }),
|
||||
|
||||
login: async (loginData) => {
|
||||
set({ status: "loading" });
|
||||
try {
|
||||
const [responseData, loginError] = await callApi(() => login(loginData));
|
||||
|
||||
if (loginError) {
|
||||
set({ status: "unauthenticated" });
|
||||
return;
|
||||
}
|
||||
|
||||
set({
|
||||
status: "authenticated",
|
||||
user: responseData?.user,
|
||||
});
|
||||
} catch (err) {
|
||||
console.warn(err);
|
||||
set({ status: "idle" });
|
||||
}
|
||||
},
|
||||
register: async (registerData) => {
|
||||
try {
|
||||
const [responseData, registerError] = await callApi(() =>
|
||||
register(registerData),
|
||||
);
|
||||
|
||||
if (registerError) {
|
||||
set({ status: "unauthenticated" });
|
||||
return;
|
||||
}
|
||||
|
||||
set({
|
||||
status: "authenticated",
|
||||
user: responseData?.user,
|
||||
});
|
||||
} catch (err) {
|
||||
console.warn(err);
|
||||
set({ status: "idle" });
|
||||
}
|
||||
},
|
||||
logout: async () => {
|
||||
set({ status: "loading" });
|
||||
try {
|
||||
const [, logoutError] = await callApi(() => logout());
|
||||
|
||||
if (logoutError) {
|
||||
set({ status: "authenticated" });
|
||||
}
|
||||
|
||||
set({ status: "unauthenticated", user: undefined });
|
||||
} catch (err) {
|
||||
console.warn(err);
|
||||
set({ status: "idle" });
|
||||
}
|
||||
},
|
||||
}));
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import type { User } from "entities/User";
|
||||
import type { AuthStatus } from "./service";
|
||||
import type { AuthData, AuthStatus } from "./service";
|
||||
|
||||
export interface AuthStoreState {
|
||||
|
||||
/**
|
||||
* User's credentials
|
||||
*/
|
||||
@@ -12,9 +13,18 @@ export interface AuthStoreState {
|
||||
status: AuthStatus;
|
||||
}
|
||||
|
||||
export type LoginAction = (data: AuthData) => void;
|
||||
export type RegisterAction = (data: AuthData) => void;
|
||||
export type LogoutAction = () => void;
|
||||
|
||||
export interface AuthStoreActions {
|
||||
setUser: (user: AuthStoreState["user"] | undefined) => void;
|
||||
setStatus: (status: AuthStoreState["status"]) => void;
|
||||
|
||||
// Async actions
|
||||
login: LoginAction;
|
||||
register: RegisterAction;
|
||||
logout: LogoutAction;
|
||||
}
|
||||
|
||||
export type AuthStore = AuthStoreState & AuthStoreActions;
|
||||
|
||||
40
src/shared/utils/helpers/callApi/callApi.spec.ts
Normal file
40
src/shared/utils/helpers/callApi/callApi.spec.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { HTTPError } from "ky";
|
||||
import { callApi } from "./callApi";
|
||||
|
||||
function makeHttpError(status: number, body: object) {
|
||||
const response = new Response(JSON.stringify(body), {
|
||||
status,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
return new HTTPError(response, new Request("http://test.com"), {});
|
||||
}
|
||||
|
||||
describe("callApi", () => {
|
||||
describe("happy path", () => {
|
||||
it("returns [data, null] when the fn resolves", async () => {
|
||||
const [data, error] = await callApi(() => Promise.resolve({ id: 1 }));
|
||||
|
||||
expect(data).toEqual({ id: 1 });
|
||||
expect(error).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("error cases", () => {
|
||||
it("returns [null, ApiError] on HTTPError", async () => {
|
||||
const [data, error] = await callApi(() =>
|
||||
Promise.reject(makeHttpError(401, { message: "Unauthorized" })),
|
||||
);
|
||||
|
||||
expect(data).toBeNull();
|
||||
expect(error).toEqual({ status: 401, message: "Unauthorized" });
|
||||
});
|
||||
|
||||
it("re-throws non-HTTP errors", async () => {
|
||||
const unexpected = new TypeError("Network failure");
|
||||
|
||||
await expect(
|
||||
callApi(() => Promise.reject(unexpected)),
|
||||
).rejects.toThrow("Network failure");
|
||||
});
|
||||
});
|
||||
});
|
||||
33
src/shared/utils/helpers/callApi/callApi.ts
Normal file
33
src/shared/utils/helpers/callApi/callApi.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { HTTPError } from "ky";
|
||||
|
||||
export interface ApiError {
|
||||
/**
|
||||
* Client error response status code
|
||||
*/
|
||||
status: number;
|
||||
/**
|
||||
* Error message
|
||||
*/
|
||||
message: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function that calls Ky and manages its errors;
|
||||
* @returns A tuple [data, error] in Golang-like fashion
|
||||
*/
|
||||
export async function callApi<T>(
|
||||
fn: () => Promise<T>,
|
||||
): Promise<[T, null] | [null, ApiError]> {
|
||||
try {
|
||||
const data = await fn();
|
||||
return [data, null];
|
||||
} catch (error) {
|
||||
if (error instanceof HTTPError) {
|
||||
const body = await error.response.json<{ message: string }>();
|
||||
return [null, { status: error.response.status, message: body.message }];
|
||||
}
|
||||
|
||||
// re-throw unexpected errors
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
1
src/shared/utils/helpers/index.ts
Normal file
1
src/shared/utils/helpers/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from "./callApi/callApi";
|
||||
1
src/shared/utils/index.ts
Normal file
1
src/shared/utils/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from "./helpers";
|
||||
Reference in New Issue
Block a user