diff --git a/libs/service/.babelrc b/libs/service/.babelrc new file mode 100644 index 0000000..1ea870e --- /dev/null +++ b/libs/service/.babelrc @@ -0,0 +1,12 @@ +{ + "presets": [ + [ + "@nx/react/babel", + { + "runtime": "automatic", + "useBuiltIns": "usage" + } + ] + ], + "plugins": [] +} diff --git a/libs/service/README.md b/libs/service/README.md new file mode 100644 index 0000000..2b18593 --- /dev/null +++ b/libs/service/README.md @@ -0,0 +1,7 @@ +# service + +This library was generated with [Nx](https://nx.dev). + +## Running unit tests + +Run `nx test service` to execute the unit tests via [Vitest](https://vitest.dev/). diff --git a/libs/service/eslint.config.mjs b/libs/service/eslint.config.mjs new file mode 100644 index 0000000..e8e4590 --- /dev/null +++ b/libs/service/eslint.config.mjs @@ -0,0 +1,12 @@ +import nx from '@nx/eslint-plugin'; +import baseConfig from '../../eslint.config.mjs'; + +export default [ + ...baseConfig, + ...nx.configs['flat/react'], + { + files: ['**/*.ts', '**/*.tsx', '**/*.js', '**/*.jsx'], + // Override or add rules here + rules: {}, + }, +]; diff --git a/libs/service/package.json b/libs/service/package.json new file mode 100644 index 0000000..e76c213 --- /dev/null +++ b/libs/service/package.json @@ -0,0 +1,12 @@ +{ + "name": "@imphnen-frontend-service/service", + "version": "0.0.1", + "main": "./index.js", + "types": "./index.d.ts", + "exports": { + ".": { + "import": "./index.mjs", + "require": "./index.js" + } + } +} diff --git a/libs/service/project.json b/libs/service/project.json new file mode 100644 index 0000000..106ad9f --- /dev/null +++ b/libs/service/project.json @@ -0,0 +1,23 @@ +{ + "name": "service", + "$schema": "../../node_modules/nx/schemas/project-schema.json", + "sourceRoot": "libs/service/src", + "projectType": "library", + "tags": [], + "targets": { + "nx-release-publish": { + "options": { + "packageRoot": "dist/{projectRoot}" + } + } + }, + "release": { + "version": { + "generatorOptions": { + "packageRoot": "dist/{projectRoot}", + "currentVersionResolver": "git-tag", + "fallbackCurrentVersionResolver": "disk" + } + } + } +} diff --git a/libs/service/src/api/auth/index.ts b/libs/service/src/api/auth/index.ts new file mode 100644 index 0000000..e925dd1 --- /dev/null +++ b/libs/service/src/api/auth/index.ts @@ -0,0 +1,41 @@ +import { api } from '@imphnen-frontend-service/utils'; +import { + TLoginRequest, + TLoginResponse, + TRegisterRequest, + TVerifyEmailRequest, +} from '../../types/auth'; +import { TResponseMessage } from '../../types/common'; + +export const postLogin = async ( + payload: TLoginRequest +): Promise => { + const { data } = await api({ + method: 'POST', + url: '/auth/login', + data: payload, + }); + return data; +}; + +export const postRegister = async ( + payload: TRegisterRequest +): Promise => { + const { data } = await api({ + method: 'POST', + url: '/auth/register', + data: payload, + }); + return data; +}; + +export const postVerifyEmail = async ( + payload: TVerifyEmailRequest +): Promise => { + const { data } = await api({ + method: 'POST', + url: '/auth/verify', + data: payload, + }); + return data; +}; diff --git a/libs/service/src/api/gacha/index.ts b/libs/service/src/api/gacha/index.ts new file mode 100644 index 0000000..cb0ff5c --- /dev/null +++ b/libs/service/src/api/gacha/index.ts @@ -0,0 +1 @@ +export {}; diff --git a/libs/service/src/api/index.ts b/libs/service/src/api/index.ts new file mode 100644 index 0000000..76d0cf2 --- /dev/null +++ b/libs/service/src/api/index.ts @@ -0,0 +1,3 @@ +export * from './auth'; +export * from './gacha'; +export * from './users'; diff --git a/libs/service/src/api/users/index.ts b/libs/service/src/api/users/index.ts new file mode 100644 index 0000000..cb0ff5c --- /dev/null +++ b/libs/service/src/api/users/index.ts @@ -0,0 +1 @@ +export {}; diff --git a/libs/service/src/hooks/auth/index.ts b/libs/service/src/hooks/auth/index.ts new file mode 100644 index 0000000..dda436e --- /dev/null +++ b/libs/service/src/hooks/auth/index.ts @@ -0,0 +1,45 @@ +import { useMutation, UseMutationResult } from '@tanstack/react-query'; +import { postLogin, postRegister, postVerifyEmail } from '../../api/auth'; +import { + TLoginRequest, + TLoginResponse, + TRegisterRequest, + TVerifyEmailRequest, +} from '../../types/auth'; +import { TResponseError, TResponseMessage } from '../../types/common'; + +export const usePostLogin = (): UseMutationResult< + TLoginResponse, + TResponseError, + TLoginRequest, + unknown +> => { + return useMutation({ + mutationKey: ['post-login'], + mutationFn: async (payload) => await postLogin(payload), + }); +}; + +export const usePostRegister = (): UseMutationResult< + TResponseMessage, + TResponseError, + TRegisterRequest, + unknown +> => { + return useMutation({ + mutationKey: ['post-register'], + mutationFn: async (payload) => await postRegister(payload), + }); +}; + +export const usePostVerifyEmail = (): UseMutationResult< + TResponseMessage, + TResponseError, + TVerifyEmailRequest, + unknown +> => { + return useMutation({ + mutationKey: ['post-verify-email'], + mutationFn: async (payload) => await postVerifyEmail(payload), + }); +}; diff --git a/libs/service/src/hooks/gacha/index.ts b/libs/service/src/hooks/gacha/index.ts new file mode 100644 index 0000000..cb0ff5c --- /dev/null +++ b/libs/service/src/hooks/gacha/index.ts @@ -0,0 +1 @@ +export {}; diff --git a/libs/service/src/hooks/index.ts b/libs/service/src/hooks/index.ts new file mode 100644 index 0000000..76d0cf2 --- /dev/null +++ b/libs/service/src/hooks/index.ts @@ -0,0 +1,3 @@ +export * from './auth'; +export * from './gacha'; +export * from './users'; diff --git a/libs/service/src/hooks/users/index.ts b/libs/service/src/hooks/users/index.ts new file mode 100644 index 0000000..cb0ff5c --- /dev/null +++ b/libs/service/src/hooks/users/index.ts @@ -0,0 +1 @@ +export {}; diff --git a/libs/service/src/index.ts b/libs/service/src/index.ts new file mode 100644 index 0000000..6c2f44a --- /dev/null +++ b/libs/service/src/index.ts @@ -0,0 +1,3 @@ +export * from './api'; +export * from './hooks'; +export * from './types'; diff --git a/libs/service/src/types/auth/index.ts b/libs/service/src/types/auth/index.ts new file mode 100644 index 0000000..bf094ec --- /dev/null +++ b/libs/service/src/types/auth/index.ts @@ -0,0 +1,29 @@ +export type TLoginRequest = { + email: string; + password: string; +}; + +export type TLoginResponse = { + data: { + token: { + access_token: string; + refresh_token: string; + }; + user: { + fullname: string; + email: string; + is_active: boolean; + }; + }; +}; + +export type TRegisterRequest = { + fullname: string; + email: string; + password: string; +}; + +export type TVerifyEmailRequest = { + email: string; + otp: number; +}; diff --git a/libs/service/src/types/common/index.ts b/libs/service/src/types/common/index.ts new file mode 100644 index 0000000..b57f062 --- /dev/null +++ b/libs/service/src/types/common/index.ts @@ -0,0 +1,11 @@ +import { AxiosError } from 'axios'; + +export type TResponse = { + data: T; +}; + +export type TResponseMessage = { + message: string; +}; + +export type TResponseError = AxiosError; diff --git a/libs/service/src/types/gacha/index.ts b/libs/service/src/types/gacha/index.ts new file mode 100644 index 0000000..cb0ff5c --- /dev/null +++ b/libs/service/src/types/gacha/index.ts @@ -0,0 +1 @@ +export {}; diff --git a/libs/service/src/types/index.ts b/libs/service/src/types/index.ts new file mode 100644 index 0000000..76d0cf2 --- /dev/null +++ b/libs/service/src/types/index.ts @@ -0,0 +1,3 @@ +export * from './auth'; +export * from './gacha'; +export * from './users'; diff --git a/libs/service/src/types/users/index.ts b/libs/service/src/types/users/index.ts new file mode 100644 index 0000000..cb0ff5c --- /dev/null +++ b/libs/service/src/types/users/index.ts @@ -0,0 +1 @@ +export {}; diff --git a/libs/service/tsconfig.json b/libs/service/tsconfig.json new file mode 100644 index 0000000..cc96381 --- /dev/null +++ b/libs/service/tsconfig.json @@ -0,0 +1,21 @@ +{ + "compilerOptions": { + "jsx": "react-jsx", + "allowJs": false, + "esModuleInterop": false, + "allowSyntheticDefaultImports": true, + "strict": true, + "types": ["vite/client", "vitest"] + }, + "files": [], + "include": [], + "references": [ + { + "path": "./tsconfig.lib.json" + }, + { + "path": "./tsconfig.spec.json" + } + ], + "extends": "../../tsconfig.base.json" +} diff --git a/libs/service/tsconfig.lib.json b/libs/service/tsconfig.lib.json new file mode 100644 index 0000000..8c540f3 --- /dev/null +++ b/libs/service/tsconfig.lib.json @@ -0,0 +1,35 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../dist/out-tsc", + "types": [ + "node", + "@nx/react/typings/cssmodule.d.ts", + "@nx/react/typings/image.d.ts", + "vite/client" + ] + }, + "exclude": [ + "**/*.spec.ts", + "**/*.test.ts", + "**/*.spec.tsx", + "**/*.test.tsx", + "**/*.spec.js", + "**/*.test.js", + "**/*.spec.jsx", + "**/*.test.jsx", + "vite.config.ts", + "vite.config.mts", + "vitest.config.ts", + "vitest.config.mts", + "src/**/*.test.ts", + "src/**/*.spec.ts", + "src/**/*.test.tsx", + "src/**/*.spec.tsx", + "src/**/*.test.js", + "src/**/*.spec.js", + "src/**/*.test.jsx", + "src/**/*.spec.jsx" + ], + "include": ["src/**/*.js", "src/**/*.jsx", "src/**/*.ts", "src/**/*.tsx"] +} diff --git a/libs/service/tsconfig.spec.json b/libs/service/tsconfig.spec.json new file mode 100644 index 0000000..56b7488 --- /dev/null +++ b/libs/service/tsconfig.spec.json @@ -0,0 +1,28 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../dist/out-tsc", + "types": [ + "vitest/globals", + "vitest/importMeta", + "vite/client", + "node", + "vitest" + ] + }, + "include": [ + "vite.config.ts", + "vite.config.mts", + "vitest.config.ts", + "vitest.config.mts", + "src/**/*.test.ts", + "src/**/*.spec.ts", + "src/**/*.test.tsx", + "src/**/*.spec.tsx", + "src/**/*.test.js", + "src/**/*.spec.js", + "src/**/*.test.jsx", + "src/**/*.spec.jsx", + "src/**/*.d.ts" + ] +} diff --git a/libs/service/vite.config.ts b/libs/service/vite.config.ts new file mode 100644 index 0000000..4f26a6e --- /dev/null +++ b/libs/service/vite.config.ts @@ -0,0 +1,59 @@ +/// +import { defineConfig } from 'vite'; +import react from '@vitejs/plugin-react'; +import dts from 'vite-plugin-dts'; +import * as path from 'path'; +import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin'; +import { nxCopyAssetsPlugin } from '@nx/vite/plugins/nx-copy-assets.plugin'; + +export default defineConfig(() => ({ + root: __dirname, + cacheDir: '../../node_modules/.vite/libs/service', + plugins: [ + react(), + nxViteTsPaths(), + nxCopyAssetsPlugin(['*.md']), + dts({ + entryRoot: 'src', + tsconfigPath: path.join(__dirname, 'tsconfig.lib.json'), + }), + ], + // Uncomment this if you are using workers. + // worker: { + // plugins: [ nxViteTsPaths() ], + // }, + // Configuration for building your library. + // See: https://vitejs.dev/guide/build.html#library-mode + build: { + outDir: '../../dist/libs/service', + emptyOutDir: true, + reportCompressedSize: true, + commonjsOptions: { + transformMixedEsModules: true, + }, + lib: { + // Could also be a dictionary or array of multiple entry points. + entry: 'src/index.ts', + name: 'service', + fileName: 'index', + // Change this to the formats you want to support. + // Don't forget to update your package.json as well. + formats: ['es' as const], + }, + rollupOptions: { + // External packages that should not be bundled into your library. + external: ['react', 'react-dom', 'react/jsx-runtime'], + }, + }, + test: { + watch: false, + globals: true, + environment: 'jsdom', + include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], + reporters: ['default'], + coverage: { + reportsDirectory: '../../coverage/libs/service', + provider: 'v8' as const, + }, + }, +})); diff --git a/tsconfig.base.json b/tsconfig.base.json index 31691d7..8fe0377 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -15,6 +15,7 @@ "skipDefaultLibCheck": true, "baseUrl": ".", "paths": { + "@imphnen-frontend-service/service": ["libs/service/src/index.ts"], "@imphnen-frontend-service/ui/atoms": ["libs/ui/src/atoms/index.ts"], "@imphnen-frontend-service/ui/molecules": [ "libs/ui/src/molecules/index.ts"