-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: split out a requests library
- Loading branch information
1 parent
1777892
commit e54efd3
Showing
15 changed files
with
274 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@siafoundation/request': minor | ||
--- | ||
|
||
Introduced a new request library for shared request and network code. |
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,12 @@ | ||
{ | ||
"presets": [ | ||
[ | ||
"@nx/react/babel", | ||
{ | ||
"runtime": "automatic", | ||
"useBuiltIns": "usage" | ||
} | ||
] | ||
], | ||
"plugins": [] | ||
} |
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,21 @@ | ||
{ | ||
"extends": ["plugin:@nx/react", "../../.eslintrc.json"], | ||
"ignorePatterns": ["!**/*"], | ||
"rules": { | ||
"@nx/dependency-checks": [ | ||
"error", | ||
{ | ||
"ignoredFiles": ["libs/request/rollup.config.js"] | ||
} | ||
] | ||
}, | ||
"overrides": [ | ||
{ | ||
"files": ["*.json"], | ||
"parser": "jsonc-eslint-parser", | ||
"rules": { | ||
"@nx/dependency-checks": "error" | ||
} | ||
} | ||
] | ||
} |
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 @@ | ||
# request | ||
|
||
Core library for building request APIs. |
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,17 @@ | ||
/* eslint-disable */ | ||
export default { | ||
displayName: 'request', | ||
preset: '../../jest.preset.js', | ||
transform: { | ||
'^(?!.*\\.(js|jsx|ts|tsx|css|json)$)': '@nx/react/plugins/jest', | ||
'^.+\\.[tj]sx?$': [ | ||
'babel-jest', | ||
{ | ||
presets: ['@nx/next/babel'], | ||
plugins: ['@babel/plugin-transform-private-methods'], | ||
}, | ||
], | ||
}, | ||
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'], | ||
coverageDirectory: '../../coverage/libs/request', | ||
} |
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 @@ | ||
{ | ||
"name": "@siafoundation/request", | ||
"description": "Core library for building request APIs.", | ||
"version": "0.0.0", | ||
"license": "MIT", | ||
"dependencies": { | ||
"axios": "^0.27.2" | ||
}, | ||
"types": "./src/index.d.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,42 @@ | ||
{ | ||
"name": "request", | ||
"$schema": "../../node_modules/nx/schemas/project-schema.json", | ||
"sourceRoot": "libs/request/src", | ||
"projectType": "library", | ||
"tags": [], | ||
"targets": { | ||
"build": { | ||
"executor": "@nx/rollup:rollup", | ||
"outputs": ["{options.outputPath}"], | ||
"options": { | ||
"outputPath": "dist/libs/request", | ||
"tsConfig": "libs/request/tsconfig.lib.json", | ||
"project": "libs/request/package.json", | ||
"entryFile": "libs/request/src/index.ts", | ||
"external": ["react/jsx-runtime"], | ||
"compiler": "tsc", | ||
"outputFileName": "index.js", | ||
"rollupConfig": "libs/request/rollup.config.js", | ||
"assets": [ | ||
{ | ||
"glob": "libs/request/*.md", | ||
"input": ".", | ||
"output": "." | ||
} | ||
] | ||
}, | ||
"configurations": {} | ||
}, | ||
"lint": { | ||
"executor": "@nx/eslint:lint", | ||
"outputs": ["{options.outputFile}"] | ||
}, | ||
"test": { | ||
"executor": "@nx/jest:jest", | ||
"outputs": ["{workspaceRoot}/coverage/libs/request"], | ||
"options": { | ||
"jestConfig": "libs/request/jest.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,18 @@ | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const preserveDirectives = require('rollup-plugin-preserve-directives') | ||
|
||
// https://github.com/rollup/rollup/issues/4699#issuecomment-1465302665 | ||
function getRollupOptions(options) { | ||
return { | ||
...options, | ||
output: { | ||
...options.output, | ||
preserveModules: true, | ||
format: 'esm', | ||
sourcemap: true, | ||
}, | ||
plugins: options.plugins.concat(preserveDirectives.default()), | ||
} | ||
} | ||
|
||
module.exports = getRollupOptions |
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,76 @@ | ||
import { Axios, AxiosRequestConfig, AxiosRequestHeaders } from 'axios' | ||
|
||
export type RequestParams = Record< | ||
string, | ||
string | string[] | number | boolean | ||
> | void | ||
|
||
export function parameterizeRoute( | ||
route: string | null, | ||
params: RequestParams | ||
): string | null { | ||
if (route && params) { | ||
const paramKeys = Object.keys(params) | ||
for (const key of paramKeys) { | ||
const value = String(params[key]) | ||
if (route.includes(`:${key}`)) { | ||
route = route.replace(`:${key}`, value) | ||
} else { | ||
if (!route.includes('?')) { | ||
route += `?${key}=${encodeURIComponent(value)}` | ||
} else { | ||
route += `&${key}=${encodeURIComponent(value)}` | ||
} | ||
} | ||
} | ||
} | ||
return route | ||
} | ||
|
||
type Method = 'get' | 'post' | 'patch' | 'put' | 'delete' | ||
|
||
export function buildRequestHandler< | ||
Params = void, | ||
Data = void, | ||
Response = void | ||
>(axios: Axios, method: Method, route: string) { | ||
type Args = Params extends void | ||
? Data extends void | ||
? { config?: AxiosRequestConfig<Data> } | void | ||
: { data: Data; config?: AxiosRequestConfig<Data> } | ||
: Data extends void | ||
? { | ||
params: Params | ||
config?: AxiosRequestConfig<Data> | ||
} | ||
: { | ||
params: Params | ||
data: Data | ||
config?: AxiosRequestConfig<Data> | ||
} | ||
|
||
return (args: Args) => { | ||
const paramRoute = | ||
args && 'params' in args | ||
? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
parameterizeRoute(route, args.params as RequestParams)! | ||
: route | ||
|
||
const data = args && 'data' in args ? args.data : undefined | ||
|
||
return axios[method]<Response>(paramRoute, data, args?.config) | ||
} | ||
} | ||
|
||
export function initAxios(api: string, password?: string): Axios { | ||
const headers: AxiosRequestHeaders = { | ||
'Content-Type': 'application/json', | ||
} | ||
if (password) { | ||
headers['Authorization'] = `Basic ${btoa(`:${password}`)}` | ||
} | ||
return new Axios({ | ||
baseURL: api, | ||
headers, | ||
}) | ||
} |
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,25 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"jsx": "react-jsx", | ||
"allowJs": true, | ||
"esModuleInterop": true, | ||
"allowSyntheticDefaultImports": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"strict": true, | ||
"noImplicitOverride": true, | ||
"noPropertyAccessFromIndexSignature": true, | ||
"noImplicitReturns": true, | ||
"noFallthroughCasesInSwitch": true | ||
}, | ||
"files": [], | ||
"include": [], | ||
"references": [ | ||
{ | ||
"path": "./tsconfig.lib.json" | ||
}, | ||
{ | ||
"path": "./tsconfig.spec.json" | ||
} | ||
] | ||
} |
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,22 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "../../dist/out-tsc", | ||
"types": [ | ||
"node", | ||
"@nx/react/typings/cssmodule.d.ts", | ||
"@nx/react/typings/image.d.ts" | ||
] | ||
}, | ||
"exclude": [ | ||
"**/*.spec.ts", | ||
"**/*.test.ts", | ||
"**/*.spec.tsx", | ||
"**/*.test.tsx", | ||
"**/*.spec.js", | ||
"**/*.test.js", | ||
"**/*.spec.jsx", | ||
"**/*.test.jsx" | ||
], | ||
"include": ["src/**/*.js", "src/**/*.jsx", "src/**/*.ts", "src/**/*.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,19 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "../../dist/out-tsc", | ||
"module": "commonjs", | ||
"types": ["jest", "node"] | ||
}, | ||
"include": [ | ||
"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" | ||
] | ||
} |
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