diff --git a/.npmignore b/.npmignore index 6085858..012a3cd 100644 --- a/.npmignore +++ b/.npmignore @@ -1,2 +1 @@ -helpers index.js diff --git a/examples/cjs/README.md b/examples/cjs/README.md deleted file mode 100644 index 799f034..0000000 --- a/examples/cjs/README.md +++ /dev/null @@ -1,9 +0,0 @@ -# cjs - -test commonjs bundle imports - -Run install & test: - -```sh -pnpm it -``` diff --git a/examples/cjs/package.json b/examples/cjs/package.json deleted file mode 100644 index 198504c..0000000 --- a/examples/cjs/package.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "name": "cjs", - "version": "1.0.0", - "description": "requete commonjs imports", - "main": "index.cjs", - "scripts": { - "test": "node index.cjs" - }, - "dependencies": { - "requete": "file:../../dist" - } -} diff --git a/package.json b/package.json index 55b5875..1147e44 100644 --- a/package.json +++ b/package.json @@ -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" @@ -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" }, diff --git a/rollup.config.mjs b/rollup.config.mjs index 9c41c39..c00da81 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -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` @@ -112,7 +112,7 @@ const setupUMDConfig = (config) => { } export default [].concat( - setupModuleConfig('index', null, { + setupModuleConfig('index', /^requete/, { default: 'index', RequestError: 'RequestError', Requete: 'Requete', @@ -121,6 +121,7 @@ export default [].concat( }), setupModuleConfig('middleware'), setupModuleConfig('adapter'), + setupModuleConfig('shared'), setupUMDConfig({ input: 'src/global.mjs', output: { diff --git a/src/adapter/Adapter.ts b/src/adapter/Adapter.ts index 4951dcc..cf944eb 100644 --- a/src/adapter/Adapter.ts +++ b/src/adapter/Adapter.ts @@ -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 diff --git a/src/adapter/FetchAdapter.ts b/src/adapter/FetchAdapter.ts index 5f03b1c..86c36e6 100644 --- a/src/adapter/FetchAdapter.ts +++ b/src/adapter/FetchAdapter.ts @@ -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 { diff --git a/src/adapter/XhrAdapter.ts b/src/adapter/XhrAdapter.ts index 5e4e2be..db9a16c 100644 --- a/src/adapter/XhrAdapter.ts +++ b/src/adapter/XhrAdapter.ts @@ -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 { diff --git a/src/adapter/__tests__/FetchAdapter.test.ts b/src/adapter/__tests__/FetchAdapter.test.ts index 1a66e35..6c3262b 100644 --- a/src/adapter/__tests__/FetchAdapter.test.ts +++ b/src/adapter/__tests__/FetchAdapter.test.ts @@ -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', () => { diff --git a/src/adapter/__tests__/XhrAdapter.test.ts b/src/adapter/__tests__/XhrAdapter.test.ts index fcccc26..b68112b 100644 --- a/src/adapter/__tests__/XhrAdapter.test.ts +++ b/src/adapter/__tests__/XhrAdapter.test.ts @@ -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', () => { @@ -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() @@ -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() @@ -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() diff --git a/src/adapter/__tests__/Adapter.test.ts b/src/adapter/__tests__/create.test.ts similarity index 100% rename from src/adapter/__tests__/Adapter.test.ts rename to src/adapter/__tests__/create.test.ts diff --git a/src/core/Requete.ts b/src/core/Requete.ts index e5987be..a319d93 100644 --- a/src/core/Requete.ts +++ b/src/core/Requete.ts @@ -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' diff --git a/src/core/__tests__/requete-exceptions.test.ts b/src/core/__tests__/requete-exceptions.test.ts index cd9f527..b0168d2 100644 --- a/src/core/__tests__/requete-exceptions.test.ts +++ b/src/core/__tests__/requete-exceptions.test.ts @@ -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( diff --git a/src/core/__tests__/requete-middleware.test.ts b/src/core/__tests__/requete-middleware.test.ts index c5d0232..68a66cf 100644 --- a/src/core/__tests__/requete-middleware.test.ts +++ b/src/core/__tests__/requete-middleware.test.ts @@ -1,4 +1,5 @@ -import { FetchAdapter } from '../../adapter' +import { FetchAdapter } from 'requete/adapter' + import { Requete } from '../Requete' describe('Requete middleware specs', () => { diff --git a/src/core/__tests__/requete-request.test.ts b/src/core/__tests__/requete-request.test.ts index 4bce032..37d0ab4 100644 --- a/src/core/__tests__/requete-request.test.ts +++ b/src/core/__tests__/requete-request.test.ts @@ -1,4 +1,5 @@ -import { FetchAdapter } from '../../adapter' +import { FetchAdapter } from 'requete/adapter' + import { Method, Requete } from '../Requete' describe('Requete request configs', () => { diff --git a/src/helpers/index.ts b/src/helpers/index.ts deleted file mode 100644 index 65e9ddb..0000000 --- a/src/helpers/index.ts +++ /dev/null @@ -1,6 +0,0 @@ -export * from './getUri' -export * from './headers' -export * from './logger' -export * from './pick' -export * from './progress' -export * from './transform' diff --git a/src/index.ts b/src/index.ts index 8ca154b..e304e6a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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' diff --git a/src/middleware/logger.ts b/src/middleware/logger.ts index 79b6409..62f1b10 100644 --- a/src/middleware/logger.ts +++ b/src/middleware/logger.ts @@ -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 diff --git a/src/shared.ts b/src/shared.ts new file mode 100644 index 0000000..102810a --- /dev/null +++ b/src/shared.ts @@ -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' diff --git a/src/core/RequestError.ts b/src/shared/RequestError.ts similarity index 89% rename from src/core/RequestError.ts rename to src/shared/RequestError.ts index 5d41061..f28e20a 100644 --- a/src/core/RequestError.ts +++ b/src/shared/RequestError.ts @@ -1,4 +1,4 @@ -import type { IContext } from './Requete' +import type { IContext } from 'requete' export class RequestError extends Error { name = 'RequestError' diff --git a/src/helpers/__tests__/__snapshots__/transform.test.ts.snap b/src/shared/__tests__/__snapshots__/transform.test.ts.snap similarity index 100% rename from src/helpers/__tests__/__snapshots__/transform.test.ts.snap rename to src/shared/__tests__/__snapshots__/transform.test.ts.snap diff --git a/src/helpers/__tests__/getUri.test.ts b/src/shared/__tests__/getUri.test.ts similarity index 100% rename from src/helpers/__tests__/getUri.test.ts rename to src/shared/__tests__/getUri.test.ts diff --git a/src/helpers/__tests__/headers.test.ts b/src/shared/__tests__/headers.test.ts similarity index 100% rename from src/helpers/__tests__/headers.test.ts rename to src/shared/__tests__/headers.test.ts diff --git a/src/helpers/__tests__/progress.test.ts b/src/shared/__tests__/progress.test.ts similarity index 100% rename from src/helpers/__tests__/progress.test.ts rename to src/shared/__tests__/progress.test.ts diff --git a/src/helpers/__tests__/transform.test.ts b/src/shared/__tests__/transform.test.ts similarity index 100% rename from src/helpers/__tests__/transform.test.ts rename to src/shared/__tests__/transform.test.ts diff --git a/src/helpers/getUri.ts b/src/shared/getUri.ts similarity index 94% rename from src/helpers/getUri.ts rename to src/shared/getUri.ts index 7605707..1e9cdbe 100644 --- a/src/helpers/getUri.ts +++ b/src/shared/getUri.ts @@ -1,4 +1,4 @@ -import type { IRequest } from '../core/Requete' +import type { IRequest } from 'requete' function stringifyUrl(target: string, query: NonNullable) { const [url, search] = target.split('?') diff --git a/src/helpers/headers.ts b/src/shared/headers.ts similarity index 100% rename from src/helpers/headers.ts rename to src/shared/headers.ts diff --git a/src/helpers/logger.ts b/src/shared/logger.ts similarity index 86% rename from src/helpers/logger.ts rename to src/shared/logger.ts index 9281a28..099ab14 100644 --- a/src/helpers/logger.ts +++ b/src/shared/logger.ts @@ -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) { @@ -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) ) } diff --git a/src/helpers/pick.ts b/src/shared/pick.ts similarity index 100% rename from src/helpers/pick.ts rename to src/shared/pick.ts diff --git a/src/helpers/progress.ts b/src/shared/progress.ts similarity index 94% rename from src/helpers/progress.ts rename to src/shared/progress.ts index dbda4e6..43b42d1 100644 --- a/src/helpers/progress.ts +++ b/src/shared/progress.ts @@ -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) diff --git a/src/helpers/transform.ts b/src/shared/transform.ts similarity index 96% rename from src/helpers/transform.ts rename to src/shared/transform.ts index 2ada92e..3e35539 100644 --- a/src/helpers/transform.ts +++ b/src/shared/transform.ts @@ -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) diff --git a/test/module-specs/.eslintrc b/test/module-specs/.eslintrc new file mode 100644 index 0000000..8eee285 --- /dev/null +++ b/test/module-specs/.eslintrc @@ -0,0 +1,7 @@ +{ + "rules": { + "@typescript-eslint/no-var-requires": "off", + "simple-import-sort/imports": "off", + "simple-import-sort/exports": "off" + } +} diff --git a/test/module-specs/README.md b/test/module-specs/README.md new file mode 100644 index 0000000..7ea8e11 --- /dev/null +++ b/test/module-specs/README.md @@ -0,0 +1,9 @@ +# module-specs + +requete module (commonjs/esm) specs + +Run install & test: + +```sh +pnpm it +``` diff --git a/test/module-specs/esm.mjs b/test/module-specs/esm.mjs new file mode 100644 index 0000000..1a9f1a0 --- /dev/null +++ b/test/module-specs/esm.mjs @@ -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.') diff --git a/examples/cjs/index.cjs b/test/module-specs/index.cjs similarity index 56% rename from examples/cjs/index.cjs rename to test/module-specs/index.cjs index 6ddd9eb..5ab4da3 100644 --- a/examples/cjs/index.cjs +++ b/test/module-specs/index.cjs @@ -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.') diff --git a/test/module-specs/package.json b/test/module-specs/package.json new file mode 100644 index 0000000..b00fceb --- /dev/null +++ b/test/module-specs/package.json @@ -0,0 +1,14 @@ +{ + "name": "module-specs", + "version": "1.0.0", + "description": "requete module (commonjs/esm) specs", + "main": "index.cjs", + "scripts": { + "test": "pnpm test:cjs && pnpm test:mjs", + "test:cjs": "node index.cjs", + "test:mjs": "node esm.mjs" + }, + "dependencies": { + "requete": "file:../../dist" + } +} diff --git a/examples/cjs/pnpm-lock.yaml b/test/module-specs/pnpm-lock.yaml similarity index 100% rename from examples/cjs/pnpm-lock.yaml rename to test/module-specs/pnpm-lock.yaml diff --git a/tsconfig.json b/tsconfig.json index fe53b2c..48fa9b5 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -17,7 +17,9 @@ "types": ["vitest/globals"], "rootDir": ".", "paths": { - "test/utils": ["./test/utils"] + "test/utils": ["./test/utils"], + "requete": ["./src/index.ts"], + "requete/*": ["./src/*.ts"] } }, "include": ["src"], diff --git a/vitest.config.ts b/vitest.config.ts index 57572f7..a889f8f 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -4,5 +4,11 @@ export default defineConfig({ test: { globals: true, setupFiles: ['./test/setup'], + alias: { + 'requete/shared': './src/shared.ts', + 'requete/adapter': './src/adapter.ts', + 'requete/middleware': './src/middleware.ts', + requete: './src/index.ts', + }, }, })