Skip to content

Commit

Permalink
feat: support formdata and add ci
Browse files Browse the repository at this point in the history
  • Loading branch information
sampullman committed Dec 22, 2022
1 parent a64b82b commit dbe95cc
Show file tree
Hide file tree
Showing 13 changed files with 1,083 additions and 1,055 deletions.
23 changes: 23 additions & 0 deletions .github/actions/env-setup/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Environment Setup
description: 'Setup PNPM and Node'
runs:
using: 'composite'
steps:
- uses: actions/setup-node@v3
with:
node-version: '16'
registry-url: 'https://registry.npmjs.org'
- name: Cache pnpm modules
uses: actions/cache@v2
with:
path: ~/.pnpm-store
key: ${{ runner.os }}-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-
- uses: pnpm/[email protected]
with:
version: 7.18.0
run_install: true
- name: Build
shell: bash
run: npm run build
43 changes: 43 additions & 0 deletions .github/workflows/pr-commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: PR Commit
on:
pull_request:
branches: [main]
env:
WORKSPACE_ROOT: .

jobs:
lint:
name: Lint
runs-on: ubuntu-20.04
steps:
- name: Checkout code into workspace directory
uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: ./.github/actions/env-setup
- name: Lint source code
run: pnpm run lint

check-format:
name: Check Format
runs-on: ubuntu-20.04
steps:
- name: Checkout code into workspace directory
uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: ./.github/actions/env-setup
- name: Check code format
run: pnpm run format:check

tests:
name: Tests
runs-on: ubuntu-20.04
steps:
- name: Checkout code into workspace directory
uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: ./.github/actions/env-setup
- name: Run unit tests
run: pnpm run test
20 changes: 2 additions & 18 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,10 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-node@v2
with:
node-version: '14'
registry-url: 'https://registry.npmjs.org'
- name: Cache pnpm modules
uses: actions/cache@v2
with:
path: ~/.pnpm-store
key: ${{ runner.os }}-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-
- uses: pnpm/[email protected]
with:
version: 6.0.2
run_install: true
- run: npm run build
- uses: ./.github/actions/env-setup
- run: npm publish --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
Expand Down
13 changes: 8 additions & 5 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
module.exports = {
preset: 'ts-jest/presets/js-with-ts-esm',
globals: {
'ts-jest': {
tsconfig: './tsconfig.spec.json',
},
transform: {
'^.+\\.(ts)$': [
'ts-jest',
{
tsconfig: './tsconfig.spec.json',
},
],
},
transformIgnorePatterns: [],
testEnvironment: 'node',
testEnvironment: 'jsdom',
};
2 changes: 1 addition & 1 deletion lib/fetchApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class FetchApi<ResponseType = Response> {
let aborter: AbortController | null = null;
if (timeout) {
aborter = new AbortController();
config.signal = aborter.signal;
config.signal = aborter?.signal;
}
const resolvedParams = resolveSearchParams(params);

Expand Down
4 changes: 3 additions & 1 deletion lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export type ResolvedRequestParams =
| Record<string, string>
| undefined;

export type RequestData = Object | FormData;

export interface FetchApiOptions<ResponseType = Response> {
/**
* API base URL prepended to requests
Expand Down Expand Up @@ -74,7 +76,7 @@ export interface FetchApiConfig {
*
* WARNING - if preset, `body`, `headers['Accept']`, and `headers['Content-Type']` are overridden
*/
data?: Object;
data?: RequestData;

/**
* URL parameters appended to `url` using URLSearchParams
Expand Down
10 changes: 8 additions & 2 deletions lib/util/request.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RequestParams, ResolvedRequestParams } from '../types';
import { RequestData, RequestParams, ResolvedRequestParams } from '../types';
import { arrayToQuery } from './array';

type Filterable = {
Expand All @@ -18,8 +18,14 @@ export function resolveSearchParams(obj: RequestParams): ResolvedRequestParams {
return obj;
}

export function prepareJson(data: any, config: RequestInit): RequestInit {
export function prepareJson(data: RequestData, config: RequestInit): RequestInit {
const { headers, ...rest } = config;
if (data instanceof FormData) {
return {
...config,
body: data,
};
}
return {
...rest,
headers: {
Expand Down
31 changes: 16 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"lib"
],
"scripts": {
"build": "rimraf dist && rollup -c rollup.config.js",
"build": "rimraf dist && rollup -c rollup.config.mjs",
"example:dev": "npm -C example run dev",
"example:build": "npm -C example run build",
"build:all": "npm run build && npm run plugin:build",
Expand All @@ -34,23 +34,24 @@
},
"homepage": "https://github.com/sampullman/fetch-api#readme",
"devDependencies": {
"@rollup/plugin-commonjs": "^21.0.2",
"@rollup/plugin-node-resolve": "^13.1.3",
"@rollup/plugin-commonjs": "^24.0.0",
"@rollup/plugin-node-resolve": "^15.0.1",
"@rollup/plugin-terser": "^0.2.1",
"@types/btoa": "^1.2.3",
"@types/jest": "^27.4.1",
"@types/jest": "^29.2.4",
"@types/node": "^18.11.17",
"abort-controller": "^3.0.0",
"btoa": "^1.2.1",
"jest": "^27.5.1",
"node-fetch": "^3.2.2",
"jest": "^29.3.1",
"jest-environment-jsdom": "^29.3.1",
"node-fetch": "^3.3.0",
"rimraf": "^3.0.2",
"rollup": "^2.70.0",
"rollup-plugin-dts": "^4.2.0",
"rollup-plugin-sourcemaps": "^0.6.3",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-typescript2": "^0.31.2",
"ts-jest": "^27.1.3",
"ts-node": "^10.7.0",
"tslib": "^2.3.1",
"typescript": "^4.5.2"
"rollup": "^3.8.0",
"rollup-plugin-dts": "^5.0.0",
"rollup-plugin-typescript2": "^0.34.1",
"ts-jest": "^29.0.3",
"ts-node": "^10.9.1",
"tslib": "^2.4.1",
"typescript": "^4.9.4"
}
}
Loading

0 comments on commit dbe95cc

Please sign in to comment.