Skip to content

Commit

Permalink
feat: optimize modules shared, adapter, middleware
Browse files Browse the repository at this point in the history
- optimize build modules:
   + add `requete/shared` (helpers)
   + refactor `requete/middleware`
   + refactor `requete/adapter`
- add specs:
  + module-specs: for test build exports
  • Loading branch information
Rexer Wang committed May 1, 2023
1 parent a21b9b1 commit 861f52f
Show file tree
Hide file tree
Showing 39 changed files with 184 additions and 67 deletions.
1 change: 0 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
helpers
index.js
9 changes: 0 additions & 9 deletions examples/cjs/README.md

This file was deleted.

12 changes: 0 additions & 12 deletions examples/cjs/package.json

This file was deleted.

6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"clean": "rimraf -rf ./dist",
"test": "vitest run",
"test:coverage": "vitest run --coverage",
"test:module": "cd ./test/module-specs && pnpm it --force",
"serve": "json-server -c ./examples/mocks/json-server.json ./examples/mocks/db.json",
"release": "release-it",
"postinstall": "husky install"
Expand All @@ -33,6 +34,11 @@
"import": "./adapter.mjs",
"require": "./adapter.cjs"
},
"./shared": {
"types": "./shared.d.ts",
"import": "./shared.mjs",
"require": "./shared.cjs"
},
"./polyfill": "./polyfill.umd.js",
"./package.json": "./package.json"
},
Expand Down
5 changes: 3 additions & 2 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const useBabel = () =>
presets: ['@babel/preset-env'],
})

const setupModuleConfig = (file, external, exports) => {
const setupModuleConfig = (file, external = /^requete/, exports = null) => {
const module = pkg.exports[file === 'index' ? '.' : `./${file}`]
const input = `src/${file}.ts`

Expand Down Expand Up @@ -112,7 +112,7 @@ const setupUMDConfig = (config) => {
}

export default [].concat(
setupModuleConfig('index', null, {
setupModuleConfig('index', /^requete/, {
default: 'index',
RequestError: 'RequestError',
Requete: 'Requete',
Expand All @@ -121,6 +121,7 @@ export default [].concat(
}),
setupModuleConfig('middleware'),
setupModuleConfig('adapter'),
setupModuleConfig('shared'),
setupUMDConfig({
input: 'src/global.mjs',
output: {
Expand Down
2 changes: 1 addition & 1 deletion src/adapter/Adapter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IContext, IResponse } from '../core/Requete'
import type { IContext, IResponse } from 'requete'

export abstract class Adapter {
static readonly supported: boolean
Expand Down
5 changes: 3 additions & 2 deletions src/adapter/FetchAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { IContext, IRequest, IResponse } from '../core/Requete'
import { pick, transformRequestBody } from '../helpers'
import type { IContext, IRequest, IResponse } from 'requete'
import { pick, transformRequestBody } from 'requete/shared'

import { Adapter } from './Adapter'

export class FetchAdapter extends Adapter {
Expand Down
7 changes: 4 additions & 3 deletions src/adapter/XhrAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { RequestError } from '../core/RequestError'
import type { IContext, IResponse } from '../core/Requete'
import { IContext, IResponse } from 'requete'
import {
parseHeaders,
progressEventReducer,
RequestError,
transformRequestBody,
} from '../helpers'
} from 'requete/shared'

import { Adapter } from './Adapter'

export interface IProgressEvent {
Expand Down
3 changes: 1 addition & 2 deletions src/adapter/__tests__/FetchAdapter.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Method, TimeoutAbortController } from 'requete'
import { toAny } from 'test/utils'

import { TimeoutAbortController } from '../../core/AbortController'
import { Method } from '../../core/Requete'
import { FetchAdapter } from '../FetchAdapter'

describe('FetchAdapter specs', () => {
Expand Down
11 changes: 5 additions & 6 deletions src/adapter/__tests__/XhrAdapter.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Method, TimeoutAbortController } from 'requete'
import { sleep, toAny } from 'test/utils'

import { TimeoutAbortController } from '../../core/AbortController'
import { Method } from '../../core/Requete'
import * as helpers from '../../helpers'
import * as progress from '../../shared/progress'
import { XhrAdapter } from '../XhrAdapter'

describe('XhrAdapter specs', () => {
Expand Down Expand Up @@ -410,7 +409,7 @@ describe('XhrAdapter specs', () => {
it('should not set `onDownloadProgress` to xhr when given the option', () => {
const handleStub = vi.fn()
const spy = vi
.spyOn(helpers, 'progressEventReducer')
.spyOn(progress, 'progressEventReducer')
.mockImplementation(toAny(vi.fn().mockReturnValue(handleStub)))

const addEventListenerStub = vi.fn()
Expand All @@ -436,7 +435,7 @@ describe('XhrAdapter specs', () => {
it('should set `onUploadProgress` to xhr when given the option & it support upload', () => {
const handleStub = vi.fn()
const spy = vi
.spyOn(helpers, 'progressEventReducer')
.spyOn(progress, 'progressEventReducer')
.mockImplementation(toAny(vi.fn().mockReturnValue(handleStub)))

const addEventListenerStub = vi.fn()
Expand All @@ -462,7 +461,7 @@ describe('XhrAdapter specs', () => {

it('should not set `onUploadProgress` to xhr when it not support upload', () => {
const spy = vi
.spyOn(helpers, 'progressEventReducer')
.spyOn(progress, 'progressEventReducer')
.mockImplementation(toAny(vi.fn()))

const addEventListenerStub = vi.fn()
Expand Down
File renamed without changes.
12 changes: 9 additions & 3 deletions src/core/Requete.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { Adapter, createAdapter } from '../adapter'
import { getUri, Logger, mergeHeaders, pick } from '../helpers'
import {
Adapter,
createAdapter,
FetchAdapter,
XhrAdapter,
} from 'requete/adapter'
import { getUri, Logger, mergeHeaders, pick } from 'requete/shared'
import { RequestError } from 'requete/shared'

import { TimeoutAbortController } from './AbortController'
import { compose } from './compose'
import { RequestError } from './RequestError'

export type Method =
| 'GET'
Expand Down
7 changes: 3 additions & 4 deletions src/core/__tests__/requete-exceptions.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Requete } from 'requete'
import { FetchAdapter } from 'requete/adapter'
import { RequestError } from 'requete/shared'
import { toAny } from 'test/utils'

import { FetchAdapter } from '../../adapter'
import { RequestError } from '../RequestError'
import { Requete } from '../Requete'

describe('Requete exceptions specs', () => {
beforeEach(() => {
vi.spyOn(FetchAdapter.prototype, 'request').mockImplementation(
Expand Down
3 changes: 2 additions & 1 deletion src/core/__tests__/requete-middleware.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FetchAdapter } from '../../adapter'
import { FetchAdapter } from 'requete/adapter'

import { Requete } from '../Requete'

describe('Requete middleware specs', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/core/__tests__/requete-request.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FetchAdapter } from '../../adapter'
import { FetchAdapter } from 'requete/adapter'

import { Method, Requete } from '../Requete'

describe('Requete request configs', () => {
Expand Down
6 changes: 0 additions & 6 deletions src/helpers/index.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ export default create()

export * from './core/AbortController'
export * from './core/create'
export * from './core/RequestError'
export * from './core/Requete'
export { RequestError } from 'requete/shared'
4 changes: 2 additions & 2 deletions src/middleware/logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Middleware } from '../core/Requete'
import { Logger } from '../helpers'
import type { Middleware } from 'requete'
import { Logger } from 'requete/shared'

interface IOption {
name?: string
Expand Down
7 changes: 7 additions & 0 deletions src/shared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export * from './shared/getUri'
export * from './shared/headers'
export * from './shared/logger'
export * from './shared/pick'
export * from './shared/progress'
export * from './shared/RequestError'
export * from './shared/transform'
2 changes: 1 addition & 1 deletion src/core/RequestError.ts → src/shared/RequestError.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IContext } from './Requete'
import type { IContext } from 'requete'

export class RequestError extends Error {
name = 'RequestError'
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/helpers/getUri.ts → src/shared/getUri.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IRequest } from '../core/Requete'
import type { IRequest } from 'requete'

function stringifyUrl(target: string, query: NonNullable<IRequest['params']>) {
const [url, search] = target.split('?')
Expand Down
File renamed without changes.
10 changes: 6 additions & 4 deletions src/helpers/logger.ts → src/shared/logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { RequestError } from '../core/RequestError'
import type { IContext } from '../core/Requete'
import type { IContext } from 'requete'

import { RequestError } from './RequestError'

export class Logger {
constructor(private name: string, private level: number) {
Expand Down Expand Up @@ -39,13 +40,14 @@ export class Logger {
if (this.level < 1) return
if (!(e instanceof RequestError)) return console.error(e)

const { request, url, status, statusText } = e.ctx
const { ctx } = e as RequestError
const { request, url, status, statusText } = ctx

console.error(
`${this.name} ${request.method} ${url} ${status} (${
status === -1 ? 'Before Request' : statusText
})\n%o\n${e.stack}`,
this.res(e.ctx)
this.res(ctx)
)
}

Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions src/helpers/progress.ts → src/shared/progress.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { IProgressEvent } from '../adapter/XhrAdapter'
import type { IProgressEvent } from 'requete/adapter'

/**
* Calculate data maxRate
*
* Forked: https://github.com/axios/axios/blob/6f360a2531d8d70363fd9becef6a45a323f170e2/lib/helpers/speedometer.js
* Forked: https://github.com/axios/axios/blob/6f360a2531d8d70363fd9becef6a45a323f170e2/lib/helper/speedometer.js
*/
function speedometer(samplesCount = 10, min = 1000) {
const bytes = new Array(samplesCount)
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/transform.ts → src/shared/transform.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { RequestBody } from '../core/Requete'
import type { RequestBody } from 'requete'

export function transformRequestBody(data?: RequestBody) {
const toString = Object.prototype.toString.call(data)
Expand Down
7 changes: 7 additions & 0 deletions test/module-specs/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"rules": {
"@typescript-eslint/no-var-requires": "off",
"simple-import-sort/imports": "off",
"simple-import-sort/exports": "off"
}
}
9 changes: 9 additions & 0 deletions test/module-specs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# module-specs

requete module (commonjs/esm) specs

Run install & test:

```sh
pnpm it
```
59 changes: 59 additions & 0 deletions test/module-specs/esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { equal } from 'assert/strict'

import requete, {
RequestError,
Requete,
TimeoutAbortController,
create,
} from 'requete'
equal(typeof requete, 'object', 'requete')
equal(typeof requete.request, 'function', 'requete.request')
equal(typeof RequestError, 'function', 'requete.RequestError')
equal(typeof Requete, 'function', 'requete.Requete')
equal(
typeof TimeoutAbortController,
'function',
'requete.TimeoutAbortController'
)
equal(typeof create, 'function', 'requete.create')

import { logger } from 'requete/middleware'
equal(typeof logger, 'function', 'requete/middleware:logger')

import {
Adapter,
FetchAdapter,
XhrAdapter,
createAdapter,
} from 'requete/adapter'
equal(typeof Adapter, 'function', 'requete/adapter:Adapter')
equal(typeof FetchAdapter, 'function', 'requete/adapter:FetchAdapter')
equal(typeof XhrAdapter, 'function', 'requete/adapter:XhrAdapter')
equal(typeof createAdapter, 'function', 'requete/adapter:createAdapter')

import {
getUri,
mergeHeaders,
parseHeaders,
Logger,
pick,
progressEventReducer,
transformRequestBody,
} from 'requete/shared'
equal(typeof getUri, 'function', 'requete/shared:getUri')
equal(typeof mergeHeaders, 'function', 'requete/shared:mergeHeaders')
equal(typeof parseHeaders, 'function', 'requete/shared:parseHeaders')
equal(typeof Logger, 'function', 'requete/shared:Logger')
equal(typeof pick, 'function', 'requete/shared:pick')
equal(
typeof progressEventReducer,
'function',
'requete/shared:progressEventReducer'
)
equal(
typeof transformRequestBody,
'function',
'requete/shared:transformRequestBody'
)

console.log('All esm exports specs passed.')
27 changes: 26 additions & 1 deletion examples/cjs/index.cjs → test/module-specs/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,29 @@ assert.equal(typeof FetchAdapter, 'function', 'requete/adapter:FetchAdapter')
assert.equal(typeof XhrAdapter, 'function', 'requete/adapter:XhrAdapter')
assert.equal(typeof createAdapter, 'function', 'requete/adapter:createAdapter')

console.log('All tests passed.')
const {
getUri,
mergeHeaders,
parseHeaders,
Logger,
pick,
progressEventReducer,
transformRequestBody,
} = require('requete/shared')
assert.equal(typeof getUri, 'function', 'requete/shared:getUri')
assert.equal(typeof mergeHeaders, 'function', 'requete/shared:mergeHeaders')
assert.equal(typeof parseHeaders, 'function', 'requete/shared:parseHeaders')
assert.equal(typeof Logger, 'function', 'requete/shared:Logger')
assert.equal(typeof pick, 'function', 'requete/shared:pick')
assert.equal(
typeof progressEventReducer,
'function',
'requete/shared:progressEventReducer'
)
assert.equal(
typeof transformRequestBody,
'function',
'requete/shared:transformRequestBody'
)

console.log('All commonjs exports specs passed.')
Loading

0 comments on commit 861f52f

Please sign in to comment.