diff --git a/.changeset/tangy-brooms-eat.md b/.changeset/tangy-brooms-eat.md new file mode 100644 index 0000000..cf43f2e --- /dev/null +++ b/.changeset/tangy-brooms-eat.md @@ -0,0 +1,5 @@ +--- +"@musica-sacra/api": major +--- + +Create new plugin musica-sacra/api diff --git a/eslint.config.js b/eslint.config.js index d360e28..a3a9430 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -42,6 +42,8 @@ export default defineConfig([ rules: { 'arrow-body-style': 'off', 'react/react-in-jsx-scope': 'off', + '@typescript-eslint/no-require-imports': 'off', + '@typescript-eslint/no-explicit-any': 'warn', }, }, -]); \ No newline at end of file +]); diff --git a/package-lock.json b/package-lock.json index 663e16e..e37fb90 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1438,6 +1438,10 @@ "react": ">=16" } }, + "node_modules/@musica-sacra/api": { + "resolved": "packages/api", + "link": true + }, "node_modules/@musica-sacra/dummy": { "resolved": "packages/dummy", "link": true @@ -3240,6 +3244,34 @@ "@swc/counter": "^0.1.3" } }, + "node_modules/@tanstack/query-core": { + "version": "5.90.7", + "resolved": "https://registry.npmjs.org/@tanstack/query-core/-/query-core-5.90.7.tgz", + "integrity": "sha512-6PN65csiuTNfBMXqQUxQhCNdtm1rV+9kC9YwWAIKcaxAauq3Wu7p18j3gQY3YIBJU70jT/wzCCZ2uqto/vQgiQ==", + "dev": true, + "license": "MIT", + "funding": { + "type": "github", + "url": "https://github.com/sponsors/tannerlinsley" + } + }, + "node_modules/@tanstack/react-query": { + "version": "5.90.7", + "resolved": "https://registry.npmjs.org/@tanstack/react-query/-/react-query-5.90.7.tgz", + "integrity": "sha512-wAHc/cgKzW7LZNFloThyHnV/AX9gTg3w5yAv0gvQHPZoCnepwqCMtzbuPbb2UvfvO32XZ46e8bPOYbfZhzVnnQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@tanstack/query-core": "5.90.7" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/tannerlinsley" + }, + "peerDependencies": { + "react": "^18 || ^19" + } + }, "node_modules/@testing-library/dom": { "version": "10.4.0", "resolved": "https://registry.npmjs.org/@testing-library/dom/-/dom-10.4.0.tgz", @@ -4429,6 +4461,13 @@ "node": ">= 0.4" } }, + "node_modules/asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==", + "dev": true, + "license": "MIT" + }, "node_modules/available-typed-arrays": { "version": "1.0.7", "resolved": "https://registry.npmjs.org/available-typed-arrays/-/available-typed-arrays-1.0.7.tgz", @@ -4445,6 +4484,18 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/axios": { + "version": "1.13.2", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.13.2.tgz", + "integrity": "sha512-VPk9ebNqPcy5lRGuSlKx752IlDatOjT9paPlm8A7yOuW2Fbvp4X3JznJtT4f0GzGLLiWE9W8onz51SqLYwzGaA==", + "dev": true, + "license": "MIT", + "dependencies": { + "follow-redirects": "^1.15.6", + "form-data": "^4.0.4", + "proxy-from-env": "^1.1.0" + } + }, "node_modules/balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", @@ -4863,6 +4914,19 @@ "dev": true, "license": "MIT" }, + "node_modules/combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "dev": true, + "license": "MIT", + "dependencies": { + "delayed-stream": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/commander": { "version": "8.3.0", "resolved": "https://registry.npmjs.org/commander/-/commander-8.3.0.tgz", @@ -5307,6 +5371,16 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=0.4.0" + } + }, "node_modules/dequal": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/dequal/-/dequal-2.0.3.tgz", @@ -6405,6 +6479,27 @@ "dev": true, "license": "ISC" }, + "node_modules/follow-redirects": { + "version": "1.15.11", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.11.tgz", + "integrity": "sha512-deG2P0JfjrTxl50XGCDyfI97ZGVCxIpfKYmfyrQ54n5FO/0gfIES8C/Psl6kWVDolizcaaxZJnTS0QSMxvnsBQ==", + "dev": true, + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "license": "MIT", + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + }, "node_modules/for-each": { "version": "0.3.5", "resolved": "https://registry.npmjs.org/for-each/-/for-each-0.3.5.tgz", @@ -6488,6 +6583,23 @@ "node": ">= 10.0.0" } }, + "node_modules/form-data": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.4.tgz", + "integrity": "sha512-KrGhL9Q4zjj0kiUt5OO4Mr/A/jlI2jDYs5eHBpYHPcBEVSiipAvn2Ko2HnPe20rmcuuvMHNdZFp+4IlGTMF0Ow==", + "dev": true, + "license": "MIT", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "es-set-tostringtag": "^2.1.0", + "hasown": "^2.0.2", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/fs-extra": { "version": "7.0.1", "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-7.0.1.tgz", @@ -9542,6 +9654,13 @@ "dev": true, "license": "MIT" }, + "node_modules/proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==", + "dev": true, + "license": "MIT" + }, "node_modules/punycode": { "version": "2.3.1", "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz", @@ -11921,6 +12040,27 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "packages/api": { + "name": "@musica-sacra/api", + "version": "0.0.0", + "license": "ISC", + "devDependencies": { + "@musica-sacra/notifications": "^1.0.1", + "@tanstack/react-query": "^5.89.0", + "axios": "^1.11.0", + "react": "^19.0.0", + "react-dom": "^19.0.0", + "tslib": "^2.8.1", + "typescript": "^5.8.2" + }, + "peerDependencies": { + "@musica-sacra/notifications": "^1.0.1", + "@tanstack/react-query": "^5.89.0", + "axios": "^1.11.0", + "react": "^19.0.0", + "react-dom": "^19.0.0" + } + }, "packages/dummy": { "name": "@musica-sacra/dummy", "version": "0.0.4", diff --git a/packages/api/.npmrc b/packages/api/.npmrc new file mode 100644 index 0000000..04f16dd --- /dev/null +++ b/packages/api/.npmrc @@ -0,0 +1,8 @@ +# Enable workspaces +enable-pre-post-scripts=true + +# Use npm workspaces +workspaces-update=true + +# Auto install peer dependencies +auto-install-peers=true \ No newline at end of file diff --git a/packages/api/README.md b/packages/api/README.md new file mode 100644 index 0000000..f5259c8 --- /dev/null +++ b/packages/api/README.md @@ -0,0 +1 @@ +# @musica-sacra/api \ No newline at end of file diff --git a/packages/api/package.json b/packages/api/package.json new file mode 100644 index 0000000..4c9799f --- /dev/null +++ b/packages/api/package.json @@ -0,0 +1,46 @@ +{ + "name": "@musica-sacra/api", + "version": "0.0.0", + "description": "Api service helpers and hooks", + "main": "dist/index.js", + "module": "dist/index.mjs", + "types": "dist/index.d.ts", + "scripts": { + "build": "rollup -c --bundleConfigAsCjs", + "test": "echo \"Error: no test specified\" && exit 1", + "lint": "eslint src --ext .ts,.tsx", + "lint:fix": "eslint src --ext .ts,.tsx --fix", + "clean": "rm -rf dist" + }, + "author": "Samuel Slávik", + "license": "ISC", + "files": [ + "dist" + ], + "keywords": [ + "musica-sacra", + "musica", + "sacra", + "api" + ], + "publishConfig": { + "access": "public" + }, + "dependencies": {}, + "devDependencies": { + "react": "^19.0.0", + "react-dom": "^19.0.0", + "tslib": "^2.8.1", + "typescript": "^5.8.2", + "axios": "^1.11.0", + "@musica-sacra/notifications": "^1.0.1", + "@tanstack/react-query": "^5.89.0" + }, + "peerDependencies": { + "react": "^19.0.0", + "react-dom": "^19.0.0", + "axios": "^1.11.0", + "@musica-sacra/notifications": "^1.0.1", + "@tanstack/react-query": "^5.89.0" + } +} \ No newline at end of file diff --git a/packages/api/rollup.config.js b/packages/api/rollup.config.js new file mode 100644 index 0000000..c5f8479 --- /dev/null +++ b/packages/api/rollup.config.js @@ -0,0 +1,3 @@ +import { createBaseConfig, createTypeConfig } from '../../rollup.config.base'; + +export default [createBaseConfig(), createTypeConfig()]; diff --git a/packages/api/src/hooks/useGetById.ts b/packages/api/src/hooks/useGetById.ts new file mode 100644 index 0000000..e81584f --- /dev/null +++ b/packages/api/src/hooks/useGetById.ts @@ -0,0 +1,29 @@ +import { useContext, useEffect, useMemo } from 'react'; +import { + NotificationsContext, + NotificationTypes, +} from '@musica-sacra/notifications'; +import { useQuery } from '@tanstack/react-query'; +import { BaseService } from '../service/BaseService'; + +export function useGetById(url: string, key: string, id: string) { + const { addNotification } = useContext(NotificationsContext); + + const baseService = useMemo(() => new BaseService(), []); + + const query = useQuery({ + queryKey: [key, id], + queryFn: async () => { + const response = await baseService.getById(url, id); + return response.data; + }, + }); + + useEffect(() => { + if (query.error) { + addNotification(query.error.message, NotificationTypes.ERROR); + } + }, [query.isError]); + + return { query }; +} diff --git a/packages/api/src/hooks/useGetList.ts b/packages/api/src/hooks/useGetList.ts new file mode 100644 index 0000000..39f0313 --- /dev/null +++ b/packages/api/src/hooks/useGetList.ts @@ -0,0 +1,33 @@ +import { useContext, useEffect, useMemo } from 'react'; +import { + NotificationsContext, + NotificationTypes, +} from '@musica-sacra/notifications'; +import { useQuery } from '@tanstack/react-query'; +import { BaseService } from '../service/BaseService'; + +export function useGetList( + url: string, + key: string, + params?: Record +) { + const { addNotification } = useContext(NotificationsContext); + + const baseService = useMemo(() => new BaseService(), []); + + const query = useQuery({ + queryKey: [key, params], + queryFn: async () => { + const response = await baseService.getList(url, params); + return response.data; + }, + }); + + useEffect(() => { + if (query.error) { + addNotification(query.error.message, NotificationTypes.ERROR); + } + }, [query.isError]); + + return { query }; +} diff --git a/packages/api/src/main.ts b/packages/api/src/main.ts new file mode 100644 index 0000000..4835e2f --- /dev/null +++ b/packages/api/src/main.ts @@ -0,0 +1,5 @@ +export { useGetList } from './hooks/useGetList'; +export { useGetById } from './hooks/useGetById'; + +export { AbstractService } from './service/AbstractService'; +export { BaseService } from './service/BaseService'; diff --git a/packages/api/src/service/AbstractService.ts b/packages/api/src/service/AbstractService.ts new file mode 100644 index 0000000..b0b53a1 --- /dev/null +++ b/packages/api/src/service/AbstractService.ts @@ -0,0 +1,22 @@ +export class AbstractService { + getUserToken() { + return localStorage.getItem('token') || ''; + } + + getHeaders() { + return { + headers: { + Authorization: `Bearer ${this.getUserToken()}`, + }, + }; + } + + getHeadersWithFile = () => { + return { + headers: { + Authorization: `Bearer ${this.getUserToken()}`, + 'Content-Type': 'multipart/form-data', + }, + }; + }; +} diff --git a/packages/api/src/service/BaseService.ts b/packages/api/src/service/BaseService.ts new file mode 100644 index 0000000..c91134b --- /dev/null +++ b/packages/api/src/service/BaseService.ts @@ -0,0 +1,20 @@ +import { AbstractService } from './AbstractService'; +import axios from 'axios'; + +type WithItems> = { items: T[] } & R; + +export class BaseService< + T = any, + R = Record, +> extends AbstractService { + async getList(url: string, params?: Record) { + return await axios.get>(url, { + params, + ...this.getHeaders(), + }); + } + + async getById(url: string, id: string) { + return await axios.get(`${url}/${id}`, { ...this.getHeaders() }); + } +} diff --git a/packages/api/tsconfig.json b/packages/api/tsconfig.json new file mode 100644 index 0000000..3d4d00f --- /dev/null +++ b/packages/api/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "rootDir": "./src", + "outDir": "dist", + }, + "include": ["src"] +} \ No newline at end of file