Skip to content

Commit

Permalink
Merge pull request #11 from glocurrency/use-get
Browse files Browse the repository at this point in the history
feat: use get
  • Loading branch information
brokeyourbike authored Sep 17, 2024
2 parents cb85a51 + 88d9d0b commit dfb800a
Show file tree
Hide file tree
Showing 23 changed files with 1,223 additions and 8 deletions.
9 changes: 3 additions & 6 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ jobs:
timeout-minutes: 5

steps:
- name: Checkout code
uses: actions/checkout@v4
- uses: actions/checkout@v4

- name: Git Identity
run: |
Expand All @@ -27,14 +26,12 @@ jobs:
with:
version: 9.3.0

- name: Setup Node.js
uses: actions/setup-node@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: 'pnpm'

- name: Install dependencies
run: pnpm install
- run: pnpm install

- name: Create Release Pull Request or Publish to npm
id: changesets
Expand Down
4 changes: 2 additions & 2 deletions packages/time/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@glocurrency/time",
"description": "Time Helpers",
"version": "0.0.0",
"version": "0.0.1",
"type": "module",
"engines": {
"node": ">=20.x"
Expand Down Expand Up @@ -32,7 +32,7 @@
"repository": {
"type": "git",
"url": "https://github.com/glocurrency/commons.ts",
"directory": "packages/storage"
"directory": "packages/time"
},
"license": "MIT"
}
4 changes: 4 additions & 0 deletions packages/ui/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dist/
coverage/
node_modules
.reports/
3 changes: 3 additions & 0 deletions packages/ui/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**/__tests__/**
src
!.npmignore
72 changes: 72 additions & 0 deletions packages/ui/package.json
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 added packages/ui/src/index.ts
Empty file.
11 changes: 11 additions & 0 deletions packages/ui/src/types.ts
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
}
18 changes: 18 additions & 0 deletions packages/ui/tsconfig.build.json
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"
]
}
23 changes: 23 additions & 0 deletions packages/ui/tsconfig.json
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"]
}
9 changes: 9 additions & 0 deletions packages/ui/vite.config.ts
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'],
},
})
11 changes: 11 additions & 0 deletions packages/ui/vitest.setup.ts
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()
})
4 changes: 4 additions & 0 deletions packages/use-get/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dist/
coverage/
node_modules
.reports/
10 changes: 10 additions & 0 deletions packages/use-get/.eslintrc.cjs
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, '../../')] },
],
},
}
4 changes: 4 additions & 0 deletions packages/use-get/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
**/__tests__/**
src
.eslintrc.cjs
!.npmignore
53 changes: 53 additions & 0 deletions packages/use-get/package.json
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"
}
}
5 changes: 5 additions & 0 deletions packages/use-get/src/cookies.ts
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')
}
53 changes: 53 additions & 0 deletions packages/use-get/src/index.ts
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,
}
}
13 changes: 13 additions & 0 deletions packages/use-get/src/types.ts
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
}
16 changes: 16 additions & 0 deletions packages/use-get/tsconfig.build.json
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"
]
}
4 changes: 4 additions & 0 deletions packages/use-get/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../../tsconfig.json",
"include": ["src/**/*.ts", "src/**/*.tsx", "*.config.ts"]
}
9 changes: 9 additions & 0 deletions packages/use-get/vite.config.ts
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'],
},
})
11 changes: 11 additions & 0 deletions packages/use-get/vitest.setup.ts
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()
})
Loading

0 comments on commit dfb800a

Please sign in to comment.