-
Notifications
You must be signed in to change notification settings - Fork 100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add proper JSDoc annotations to all public api and generate TS … #33
Changes from all commits
3d8d03f
ddc4eac
2a7c334
8ed737f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ | |
* @public | ||
* @typedef Options | ||
* @property {string} [url] the URL to request | ||
* @property {string} [method="get"] HTTP method, case-insensitive | ||
* @property {'get'|'post'|'put'|'patch'|'delete'|'options'|'head'|'GET'|'POST'|'PUT'|'PATCH'|'DELETE'|'OPTIONS'|'HEAD'} [method="get"] HTTP method, case-insensitive | ||
* @property {Headers} [headers] Request headers | ||
* @property {FormData|string|object} [body] a body, optionally encoded, to send | ||
* @property {'text'|'json'|'stream'|'blob'|'arrayBuffer'|'formData'|'stream'} [responseType="text"] An encoding to use for the response | ||
|
@@ -24,6 +24,8 @@ | |
* @property {string} [xsrfHeaderName] The name of a header to use for passing XSRF cookies | ||
* @property {(status: number) => boolean} [validateStatus] Override status code handling (default: 200-399 is a success) | ||
* @property {Array<(body: any, headers: Headers) => any?>} [transformRequest] An array of transformations to apply to the outgoing request | ||
* @property {typeof window.fetch} [fetch] Custom window.fetch implementation | ||
* @property {any} [data] | ||
*/ | ||
|
||
/** | ||
|
@@ -34,20 +36,30 @@ | |
|
||
/** | ||
* @public | ||
* @template T | ||
* @typedef Response | ||
* @property {number} status | ||
* @property {string} statusText | ||
* @property {Options} config the request configuration | ||
* @property {any} data the decoded response body | ||
* @property {T} data the decoded response body | ||
* @property {Headers} headers | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added all fields that are assigned to the response to properly match implementation (dunno if it's on purpose, or we wanna strictly "match" axios response - in that case those need to be removed + omit those within implementation) |
||
* @property {boolean} redirect | ||
* @property {string} url | ||
* @property {ResponseType} type | ||
* @property {ReadableStream<Uint8Array> | null} body | ||
* @property {boolean} bodyUsed | ||
*/ | ||
|
||
/** */ | ||
export default (function create(defaults) { | ||
export default (function create(/** @type {Options} */ defaults) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it's easier to break the export and declaration out, I think microbundle+terser will collapse them all the same: /** @param {Options} defaults */
function create() {
// ...
}
export default create(); |
||
defaults = defaults || {}; | ||
|
||
/** | ||
* Creates a request factory bound to the given HTTP method. | ||
* @private | ||
* @template T | ||
* @param {string} method | ||
* @returns {(url: string, config?: Options) => Promise<Response>} | ||
* @returns {<T=any>(url: string, config?: Options) => Promise<Response<T>>} | ||
*/ | ||
function createBodylessMethod(method) { | ||
return (url, config) => redaxios(url, Object.assign({ method }, config)); | ||
|
@@ -56,16 +68,19 @@ export default (function create(defaults) { | |
/** | ||
* Creates a request factory bound to the given HTTP method. | ||
* @private | ||
* @template T | ||
* @param {string} method | ||
* @returns {(url: string, body?: any, config?: Options) => Promise<Response>} | ||
* @returns {<T=any>(url: string, body?: any, config?: Options) => Promise<Response<T>>} | ||
*/ | ||
function createBodyMethod(method) { | ||
return (url, data, config) => redaxios(url, Object.assign({ method, data }, config)); | ||
} | ||
|
||
|
||
/** | ||
* @public | ||
* @type {((config?: Options) => Promise<Response>) | ((url: string, config?: Options) => Promise<Response>)} | ||
* @template T | ||
* @type {(<T = any>(config?: Options) => Promise<Response<T>>) | (<T = any>(url: string, config?: Options) => Promise<Response<T>>)} | ||
*/ | ||
redaxios.request = redaxios; | ||
|
||
|
@@ -90,19 +105,31 @@ export default (function create(defaults) { | |
/** @public */ | ||
redaxios.all = Promise.all; | ||
|
||
/** @public */ | ||
redaxios.spread = function (fn) { | ||
/** | ||
* @public | ||
* @template T,R | ||
* @param {(...args: T[]) => R} fn | ||
* @returns {(array: T[]) => R} | ||
*/ | ||
redaxios.spread = function(fn) { | ||
return function (results) { | ||
return fn.apply(this, results); | ||
}; | ||
}; | ||
|
||
/** | ||
* @private | ||
* @param {Record<string,any>} opts | ||
* @param {Record<string,any>} overrides | ||
* @param {boolean} [lowerCase] | ||
* @returns {Record<string,any>} | ||
*/ | ||
function deepMerge(opts, overrides, lowerCase) { | ||
if (Array.isArray(opts)) { | ||
return opts.concat(overrides); | ||
} | ||
if (overrides && typeof overrides == 'object') { | ||
let out = {}, | ||
let out = /** @type {Record<string,any>} */({}), | ||
i; | ||
if (opts) { | ||
for (i in opts) { | ||
|
@@ -123,15 +150,19 @@ export default (function create(defaults) { | |
/** | ||
* Issues a request. | ||
* @public | ||
* @param {string} [url] | ||
* @template T | ||
* @param {string | Options} url | ||
* @param {Options} [config] | ||
* @returns {Promise<Response>} | ||
* @returns {Promise<Response<T>>} | ||
*/ | ||
function redaxios(url, config) { | ||
if (typeof url !== 'string') { | ||
config = url; | ||
url = config.url; | ||
} | ||
/** | ||
* @type {Options} | ||
*/ | ||
const options = deepMerge(defaults, config || {}); | ||
let data = options.data; | ||
|
||
|
@@ -145,6 +176,9 @@ export default (function create(defaults) { | |
} | ||
|
||
const fetchFunc = options.fetch || fetch; | ||
/** | ||
* @type {{'Content-Type':'application/json';Authorization: string} & Headers} | ||
*/ | ||
const customHeaders = {}; | ||
|
||
if (data && typeof data === 'object') { | ||
|
@@ -170,9 +204,9 @@ export default (function create(defaults) { | |
url = new URL(url, options.baseURL); | ||
} | ||
|
||
/** @type {Response} */ | ||
/** @type {Response<any>} */ | ||
const response = {}; | ||
response.config = config; | ||
response.config = /** @type {Options} */ (config); | ||
|
||
return fetchFunc(url, { | ||
method: options.method, | ||
|
@@ -196,8 +230,15 @@ export default (function create(defaults) { | |
}); | ||
} | ||
|
||
redaxios.CancelToken = self.AbortController || Object; | ||
/** | ||
* @public | ||
* @type {AbortController} | ||
*/ | ||
redaxios.CancelToken = /** @type {any} */ (self).AbortController || Object; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is TypeScript dom types issue as |
||
|
||
/** | ||
* @public | ||
*/ | ||
redaxios.create = create; | ||
|
||
return redaxios; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
{ | ||
"compilerOptions": { | ||
/* Visit https://aka.ms/tsconfig.json to read more about this file */ | ||
|
||
/* Basic Options */ | ||
// "incremental": true, /* Enable incremental compilation */ | ||
"target": "ESNext", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */ | ||
"module": "ESNext", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */ | ||
"lib": ["DOM","ESNext"], /* Specify library files to be included in the compilation. */ | ||
"allowJs": true, /* Allow javascript files to be compiled. */ | ||
"checkJs": true, /* Report errors in .js files. */ | ||
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ | ||
"declaration": true, /* Generates corresponding '.d.ts' file. */ | ||
"emitDeclarationOnly": true, | ||
"declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ | ||
"declarationDir": "./dist", | ||
// "sourceMap": true, /* Generates corresponding '.map' file. */ | ||
// "outFile": "./", /* Concatenate and emit output to single file. */ | ||
// "outDir": "./ts-out", /* Redirect output structure to the directory. */ | ||
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ | ||
// "composite": true, /* Enable project compilation */ | ||
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */ | ||
// "removeComments": true, /* Do not emit comments to output. */ | ||
"noEmit": false, /* Do not emit outputs. */ | ||
// "importHelpers": true, /* Import emit helpers from 'tslib'. */ | ||
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */ | ||
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ | ||
|
||
/* Strict Type-Checking Options */ | ||
"strict": true, /* Enable all strict type-checking options. */ | ||
"suppressImplicitAnyIndexErrors": true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. easy up strict checks |
||
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */ | ||
// "strictNullChecks": true, /* Enable strict null checks. */ | ||
// "strictFunctionTypes": true, /* Enable strict checking of function types. */ | ||
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */ | ||
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */ | ||
"noImplicitThis": false, /* Raise error on 'this' expressions with an implied 'any' type. */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. easy up strict checks |
||
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */ | ||
|
||
/* Additional Checks */ | ||
// "noUnusedLocals": true, /* Report errors on unused locals. */ | ||
// "noUnusedParameters": true, /* Report errors on unused parameters. */ | ||
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ | ||
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ | ||
|
||
/* Module Resolution Options */ | ||
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ | ||
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ | ||
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ | ||
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ | ||
// "typeRoots": [], /* List of folders to include type definitions from. */ | ||
// "types": [], /* Type declaration files to be included in compilation. */ | ||
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ | ||
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ | ||
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ | ||
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ | ||
|
||
/* Source Map Options */ | ||
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */ | ||
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ | ||
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */ | ||
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */ | ||
|
||
/* Experimental Options */ | ||
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */ | ||
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */ | ||
|
||
/* Advanced Options */ | ||
"skipLibCheck": true, /* Skip type checking of declaration files. */ | ||
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */ | ||
}, | ||
"include": [ | ||
"src" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. enable on |
||
] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this doesn't have to be as direct dep, microbundle will handle that in future