-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from glocurrency/use-get
feat: use get
- Loading branch information
Showing
23 changed files
with
1,223 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
dist/ | ||
coverage/ | ||
node_modules | ||
.reports/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
**/__tests__/** | ||
src | ||
!.npmignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
{ | ||
"name": "@glocurrency/ui", | ||
"description": "React components", | ||
"version": "0.0.0", | ||
"type": "module", | ||
"engines": { | ||
"node": ">=20.x" | ||
}, | ||
"main": "./dist/index.cjs", | ||
"sideEffects": false, | ||
"module": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"exports": { | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"require": "./dist/index.cjs", | ||
"default": "./dist/index.js" | ||
} | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"size-limit": [ | ||
{ | ||
"path": [ | ||
"dist/**/*.js", | ||
"!*.d.ts", | ||
"!*.cjs" | ||
], | ||
"limit": "600 kB", | ||
"webpack": false, | ||
"brotli": true, | ||
"running": false | ||
} | ||
], | ||
"scripts": { | ||
"prebuild": "shx rm -rf dist", | ||
"typecheck": "tsc --noEmit", | ||
"type:generate": "tsc --declaration -p tsconfig.build.json", | ||
"build": "vite build --config vite.config.ts && pnpm run type:generate", | ||
"build:profile": "npx vite-bundle-visualizer -c vite.config.ts", | ||
"test:unit": "vitest --run --config vite.config.ts", | ||
"test:unit:coverage": "pnpm test:unit --coverage", | ||
"size": "size-limit" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/glocurrency/commons.ts", | ||
"directory": "packages/storage" | ||
}, | ||
"license": "MIT", | ||
"dependencies": { | ||
"@emotion/styled": "^11.13.0", | ||
"@fortawesome/free-solid-svg-icons": "^6.6.0", | ||
"@fortawesome/react-fontawesome": "^0.2.2", | ||
"@glocurrency/time": "^0.0.0", | ||
"@shopify/polaris": "^13.9.0", | ||
"query-string": "^9.1.0", | ||
"react": "^18.3.1", | ||
"react-dom": "^18.3.1", | ||
"react-syntax-highlighter": "^15.5.0" | ||
}, | ||
"devDependencies": { | ||
"@testing-library/jest-dom": "^6.5.0", | ||
"@testing-library/react": "^16.0.1", | ||
"@types/jest": "29.5.12", | ||
"@types/node": "20.14.2", | ||
"@types/react": "18.3.3", | ||
"@types/react-dom": "18.3.0", | ||
"@types/react-syntax-highlighter": "^15.5.13" | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export interface BasicEventData { | ||
app: string | ||
eventType: string | ||
targetId?: string | ||
targetType: string | ||
actorId?: string | ||
actorType: string | ||
payload?: string | ||
prevPayload?: string | ||
timestamp: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"noEmit": false, | ||
"emitDeclarationOnly": true, | ||
"rootDir": "src", | ||
"outDir": "dist" | ||
}, | ||
"exclude": [ | ||
"*.config.ts", | ||
"*.setup.ts", | ||
"**/__tests__", | ||
"**/__mocks__", | ||
"**/__stories__", | ||
"src/**/*.test.tsx", | ||
"vitest.setup.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"extends": "@scaleway/tsconfig", | ||
"compilerOptions": { | ||
"target": "esnext", | ||
"module": "esnext", | ||
"jsxImportSource": "@emotion/react", | ||
"types": ["vite/client", "@testing-library/jest-dom"], | ||
"noEmit": true, | ||
"skipLibCheck": true, | ||
// Should be removed once we clear all errors | ||
"noUncheckedIndexedAccess": false | ||
}, | ||
"include": [ | ||
"**/*.ts", | ||
"**/*.tsx", | ||
".storybook/**/*.ts", | ||
".storybook/**/*.tsx", | ||
"global.d.ts", | ||
"expect.d.ts", | ||
"jest-axe.d.ts" | ||
], | ||
"exclude": ["node_modules", "coverage", "dist", "examples"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { defineConfig, mergeConfig } from 'vite' | ||
import { defaultConfig } from '../../vite.config' | ||
|
||
export default mergeConfig(defineConfig(defaultConfig), { | ||
test: { | ||
environment: 'jsdom', | ||
setupFiles: ['./vitest.setup.ts'], | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* eslint-disable eslint-comments/disable-enable-pair */ | ||
import * as matchers from '@testing-library/jest-dom/matchers' | ||
import '@testing-library/jest-dom/vitest' | ||
import { cleanup } from '@testing-library/react' | ||
import { afterEach, expect } from 'vitest' | ||
|
||
expect.extend(matchers) | ||
|
||
afterEach(() => { | ||
cleanup() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
dist/ | ||
coverage/ | ||
node_modules | ||
.reports/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const { join } = require('path') | ||
|
||
module.exports = { | ||
rules: { | ||
'import/no-extraneous-dependencies': [ | ||
'error', | ||
{ packageDir: [__dirname, join(__dirname, '../../')] }, | ||
], | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
**/__tests__/** | ||
src | ||
.eslintrc.cjs | ||
!.npmignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
{ | ||
"name": "@glocurrency/use-get", | ||
"version": "0.0.0", | ||
"description": "A React hook to interact with APIs", | ||
"engines": { | ||
"node": ">=20.x" | ||
}, | ||
"sideEffects": false, | ||
"type": "module", | ||
"main": "./dist/index.cjs", | ||
"module": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"exports": { | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"require": "./dist/index.cjs", | ||
"default": "./dist/index.js" | ||
} | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"scripts": { | ||
"prebuild": "shx rm -rf dist", | ||
"typecheck": "tsc --noEmit", | ||
"type:generate": "tsc --declaration -p tsconfig.build.json", | ||
"build": "vite build --config vite.config.ts && pnpm run type:generate", | ||
"build:profile": "npx vite-bundle-visualizer -c vite.config.ts", | ||
"lint": "eslint --report-unused-disable-directives --cache --cache-strategy content --ext ts,tsx .", | ||
"test:unit": "vitest --run --config vite.config.ts", | ||
"test:unit:coverage": "pnpm test:unit --coverage" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/glocurrency/commons.ts", | ||
"directory": "packages/use-get" | ||
}, | ||
"license": "MIT", | ||
"peerDependencies": { | ||
"react": ">=18.0" | ||
}, | ||
"devDependencies": { | ||
"@testing-library/jest-dom": "^6.5.0", | ||
"@testing-library/react": "^16.0.1", | ||
"@types/js-cookie": "^3.0.6", | ||
"react": "18.3.1" | ||
}, | ||
"dependencies": { | ||
"axios": "^1.7.7", | ||
"js-cookie": "^3.0.5", | ||
"swr": "^2.2.5" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import cookies from 'js-cookie' | ||
|
||
export const getCloudflareAuthCookie = () => { | ||
return cookies.get('CF_Authorization') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import useSWR from 'swr' | ||
import axios from 'axios' | ||
import { ApiResponse } from './types' | ||
import { getCloudflareAuthCookie } from './cookies' | ||
|
||
function defaultHeaders(): Record<string, string> { | ||
const headers: Record<string, string> = { | ||
'X-Requested-With': 'XMLHttpRequest', | ||
} | ||
|
||
const authCookie = getCloudflareAuthCookie() | ||
if (authCookie) { | ||
headers['Authorization'] = `Bearer ${authCookie}` | ||
} | ||
|
||
return headers | ||
} | ||
|
||
function makeGatewayUrl(url: string): string { | ||
return `${process.env['NEXT_PUBLIC_API_GATEWAY_URL']}${url}` | ||
} | ||
|
||
async function axiosFetcher(url: string) { | ||
return await axios | ||
.get(url, { | ||
headers: defaultHeaders(), | ||
}) | ||
.then((res) => res.data) | ||
} | ||
|
||
export async function fetchBlob(url: string): Promise<Blob> { | ||
return fetch(url, { | ||
method: 'GET', | ||
headers: defaultHeaders(), | ||
}).then((response) => response.blob()) | ||
} | ||
|
||
export function useGet<T>(url: string) { | ||
const { data, error, mutate, isValidating, isLoading } = useSWR< | ||
ApiResponse<T> | ||
>(makeGatewayUrl(url), axiosFetcher, { | ||
revalidateIfStale: true, | ||
revalidateOnFocus: false, | ||
revalidateOnReconnect: true, | ||
}) | ||
|
||
return { | ||
response: data, | ||
mutate: mutate, | ||
isLoading: isLoading || isValidating, | ||
isError: error || !data, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export interface ValidationErrors { | ||
readonly [key: string]: string | ||
} | ||
|
||
export interface ApiErrorResponse { | ||
readonly code?: number | ||
readonly message?: string | ||
readonly errors?: ValidationErrors | ||
} | ||
|
||
export interface ApiResponse<T> extends ApiErrorResponse { | ||
readonly data?: T | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"noEmit": false, | ||
"emitDeclarationOnly": true, | ||
"rootDir": "src", | ||
"outDir": "dist" | ||
}, | ||
"exclude": [ | ||
"*.config.ts", | ||
"*.setup.ts", | ||
"**/__tests__", | ||
"**/__mocks__", | ||
"src/**/*.test.tsx" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"include": ["src/**/*.ts", "src/**/*.tsx", "*.config.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { defineConfig, mergeConfig } from 'vite' | ||
import { defaultConfig } from '../../vite.config' | ||
|
||
export default mergeConfig(defineConfig(defaultConfig), { | ||
test: { | ||
environment: 'jsdom', | ||
setupFiles: ['./vitest.setup.ts'], | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* eslint-disable eslint-comments/disable-enable-pair */ | ||
import * as matchers from '@testing-library/jest-dom/matchers' | ||
import '@testing-library/jest-dom/vitest' | ||
import { cleanup } from '@testing-library/react' | ||
import { afterEach, expect } from 'vitest' | ||
|
||
expect.extend(matchers) | ||
|
||
afterEach(() => { | ||
cleanup() | ||
}) |
Oops, something went wrong.