@forgaia/fetch
Advanced tools
Sorry, the diff of this file is not supported yet
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project version="4"> | ||
| <component name="JavaScriptSettings"> | ||
| <option name="languageLevel" value="ES6" /> | ||
| </component> | ||
| </project> |
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project version="4"> | ||
| <component name="ProjectModuleManager"> | ||
| <modules> | ||
| <module fileurl="file://$PROJECT_DIR$/.idea/fetch.iml" filepath="$PROJECT_DIR$/.idea/fetch.iml" /> | ||
| </modules> | ||
| </component> | ||
| </project> |
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project version="4"> | ||
| <component name="VcsDirectoryMappings"> | ||
| <mapping directory="$PROJECT_DIR$" vcs="Git" /> | ||
| </component> | ||
| </project> |
+52
-72
@@ -1,61 +0,39 @@ | ||
| "use strict"; | ||
| var __assign = (this && this.__assign) || function () { | ||
| __assign = Object.assign || function(t) { | ||
| for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
| s = arguments[i]; | ||
| for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
| t[p] = s[p]; | ||
| } | ||
| return t; | ||
| }; | ||
| return __assign.apply(this, arguments); | ||
| }; | ||
| var __importDefault = (this && this.__importDefault) || function (mod) { | ||
| return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
| }; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| var HttpError_1 = __importDefault(require("./HttpError")); | ||
| var FetchAPI = /** @class */ (function () { | ||
| function FetchAPI() { | ||
| import HttpError from './HttpError'; | ||
| export default class FetchAPI { | ||
| static setShouldMockAllRequests(shouldMockAllRequests) { | ||
| this.shouldMockAllRequests = shouldMockAllRequests; | ||
| } | ||
| FetchAPI.setShouldMockAllRequests = function (shouldMockAllRequests) { | ||
| this.shouldMockAllRequests = shouldMockAllRequests; | ||
| }; | ||
| FetchAPI.setBaseUrl = function (baseUrl) { | ||
| static setBaseUrl(baseUrl) { | ||
| this.baseUrl = baseUrl; | ||
| }; | ||
| FetchAPI.setDefaultHeaders = function (headers) { | ||
| Object.keys(headers).forEach(function (key) { | ||
| } | ||
| static setDefaultHeaders(headers) { | ||
| Object.keys(headers).forEach((key) => { | ||
| FetchAPI.defaultHeaders[key] = headers[key]; | ||
| }); | ||
| }; | ||
| FetchAPI.setAuthorizationToken = function (token) { | ||
| FetchAPI.setDefaultHeaders({ Authorization: "Bearer " + token }); | ||
| }; | ||
| Object.defineProperty(FetchAPI, "setDefaultRequestInfo", { | ||
| set: function (reqInfo) { | ||
| Object.keys(reqInfo).forEach(function (key) { | ||
| // @ts-ignore | ||
| FetchAPI.defaultRequestInfo[key] = reqInfo[key]; | ||
| }); | ||
| }, | ||
| enumerable: true, | ||
| configurable: true | ||
| }); | ||
| FetchAPI.serializeQueryString = function (params) { | ||
| var urlSearchParams = Object.keys(params).map(function (key) { | ||
| } | ||
| static setAuthorizationToken(token) { | ||
| FetchAPI.setDefaultHeaders({ Authorization: `Bearer ${token}` }); | ||
| } | ||
| static set setDefaultRequestInfo(reqInfo) { | ||
| Object.keys(reqInfo).forEach((key) => { | ||
| // @ts-ignore | ||
| FetchAPI.defaultRequestInfo[key] = reqInfo[key]; | ||
| }); | ||
| } | ||
| static serializeQueryString(params) { | ||
| const urlSearchParams = Object.keys(params).map(key => { | ||
| // Encode objects and array to json. (used for cellIds and drillParams.) | ||
| var value = typeof params[key] === 'object' ? JSON.stringify(params[key]) : params[key]; | ||
| const value = typeof params[key] === 'object' ? JSON.stringify(params[key]) : params[key]; | ||
| return [encodeURIComponent(key), encodeURIComponent(value)].join('='); | ||
| }); | ||
| return urlSearchParams.join('&'); | ||
| }; | ||
| FetchAPI.getUrl = function (req) { | ||
| } | ||
| static getUrl(req) { | ||
| if (req.params) { | ||
| return "" + this.baseUrl + req.url + "?" + this.serializeQueryString(req.params); | ||
| return `${this.baseUrl}${req.url}?${this.serializeQueryString(req.params)}`; | ||
| } | ||
| return "" + this.baseUrl + req.url; | ||
| }; | ||
| FetchAPI.getHeaders = function (req) { | ||
| var headers = new Headers(this.defaultHeaders); | ||
| return `${this.baseUrl}${req.url}`; | ||
| } | ||
| static getHeaders(req) { | ||
| const headers = new Headers(this.defaultHeaders); | ||
| if (this.shouldMockAllRequests || req.shouldMockRequest) { | ||
@@ -65,11 +43,15 @@ headers.set('x-mock-response', 'yes'); | ||
| return headers; | ||
| }; | ||
| FetchAPI.fetch = function (req) { | ||
| var url = this.getUrl(req); | ||
| return fetch(url, __assign(__assign(__assign({}, this.defaultRequestInfo), req), { headers: this.getHeaders(req) })) | ||
| .then(function (response) { | ||
| } | ||
| static fetch(req) { | ||
| const url = this.getUrl(req); | ||
| return fetch(url, { | ||
| ...this.defaultRequestInfo, | ||
| ...req, | ||
| headers: this.getHeaders(req) | ||
| }) | ||
| .then(response => { | ||
| if (response.ok) { | ||
| return response.json(); | ||
| } | ||
| return response.json().then(function (json) { | ||
| return response.json().then(json => { | ||
| if (json.status_code) { | ||
@@ -82,21 +64,19 @@ // The API provides custom response on errors. As shows in the example below. This code handles this situation by creating custom HttpError Class which allows sentry.io and AppDynamics to capture the error information. | ||
| // } | ||
| return Promise.reject(new HttpError_1.default(json.status_code, response.url, json.message, json.title)); | ||
| return Promise.reject(new HttpError(json.status_code, response.url, json.message, json.title)); | ||
| } | ||
| return Promise.reject(new HttpError_1.default(response.status, response.url, response.statusText)); | ||
| return Promise.reject(new HttpError(response.status, response.url, response.statusText)); | ||
| }); | ||
| }) | ||
| .catch(function (error) { | ||
| throw new HttpError_1.default(error.code || 500, url, error.message || '', error.friendlyMessage); | ||
| .catch(error => { | ||
| throw new HttpError(error.code || 500, url, error.message || '', error.friendlyMessage); | ||
| }); | ||
| }; | ||
| FetchAPI.shouldMockAllRequests = false; | ||
| FetchAPI.defaultHeaders = { | ||
| 'Access-Control-Allow-Origin': '*', | ||
| 'Content-Type': 'application/json' | ||
| }; | ||
| FetchAPI.defaultRequestInfo = { | ||
| method: 'GET' | ||
| }; | ||
| return FetchAPI; | ||
| }()); | ||
| exports.default = FetchAPI; | ||
| } | ||
| } | ||
| FetchAPI.shouldMockAllRequests = false; | ||
| FetchAPI.defaultHeaders = { | ||
| 'Access-Control-Allow-Origin': '*', | ||
| 'Content-Type': 'application/json' | ||
| }; | ||
| FetchAPI.defaultRequestInfo = { | ||
| method: 'GET' | ||
| }; |
+14
-33
@@ -1,17 +0,2 @@ | ||
| "use strict"; | ||
| var __extends = (this && this.__extends) || (function () { | ||
| var extendStatics = function (d, b) { | ||
| extendStatics = Object.setPrototypeOf || | ||
| ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
| function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
| return extendStatics(d, b); | ||
| }; | ||
| return function (d, b) { | ||
| extendStatics(d, b); | ||
| function __() { this.constructor = d; } | ||
| d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
| }; | ||
| })(); | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.errorMessages = { | ||
| export const errorMessages = { | ||
| 401: 'Unauthorized, Your token has expired and needs to be renews.', | ||
@@ -22,4 +7,3 @@ 404: 'Not Found', | ||
| }; | ||
| var HttpError = /** @class */ (function (_super) { | ||
| __extends(HttpError, _super); | ||
| export default class HttpError extends Error { | ||
| /** | ||
@@ -31,19 +15,16 @@ * @param {number} httpCode | ||
| */ | ||
| function HttpError(httpCode, url, statusText, friendlyMessage) { | ||
| var _this = _super.call(this) || this; | ||
| _this.url = undefined; | ||
| _this.code = undefined; | ||
| _this.friendlyMessage = undefined; | ||
| _this.name = 'HTTP Error'; | ||
| _this.code = httpCode; | ||
| _this.url = url; | ||
| _this.message = statusText || ''; | ||
| _this.friendlyMessage = friendlyMessage || exports.errorMessages[_this.code] || 'Something went wrong.'; | ||
| constructor(httpCode, url, statusText, friendlyMessage) { | ||
| super(); | ||
| this.url = undefined; | ||
| this.code = undefined; | ||
| this.friendlyMessage = undefined; | ||
| this.name = 'HTTP Error'; | ||
| this.code = httpCode; | ||
| this.url = url; | ||
| this.message = statusText || ''; | ||
| this.friendlyMessage = friendlyMessage || errorMessages[this.code] || 'Something went wrong.'; | ||
| if (Error.captureStackTrace) { | ||
| Error.captureStackTrace(_this, HttpError); | ||
| Error.captureStackTrace(this, HttpError); | ||
| } | ||
| return _this; | ||
| } | ||
| return HttpError; | ||
| }(Error)); | ||
| exports.default = HttpError; | ||
| } |
+0
-1
| export { default } from './FetchAPI'; | ||
| export { default as HttpError } from './HttpError'; | ||
| export * from './HttpError'; | ||
| export * from './FetchAPI'; |
+3
-10
@@ -1,10 +0,3 @@ | ||
| "use strict"; | ||
| function __export(m) { | ||
| for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
| } | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| var FetchAPI_1 = require("./FetchAPI"); | ||
| exports.default = FetchAPI_1.default; | ||
| var HttpError_1 = require("./HttpError"); | ||
| exports.HttpError = HttpError_1.default; | ||
| __export(require("./FetchAPI")); | ||
| export { default } from './FetchAPI'; | ||
| export * from './HttpError'; | ||
| export * from './FetchAPI'; |
+1
-1
| { | ||
| "name": "@forgaia/fetch", | ||
| "version": "1.0.5", | ||
| "version": "1.0.6", | ||
| "description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
-105
| import HttpError from './HttpError'; | ||
| interface RequestParams extends Partial<Omit<Request, 'body'>> { | ||
| readonly url: string; | ||
| readonly params?: Record<string, any>; | ||
| readonly shouldMockRequest?: boolean; | ||
| readonly body?: string; | ||
| } | ||
| export default class FetchAPI { | ||
| private static shouldMockAllRequests: boolean = false; | ||
| private static baseUrl: string; | ||
| private static defaultHeaders: Record<string, string> = { | ||
| 'Access-Control-Allow-Origin': '*', | ||
| 'Content-Type': 'application/json' | ||
| }; | ||
| private static defaultRequestInfo: Partial<RequestParams> = { | ||
| method: 'GET' | ||
| }; | ||
| public static setShouldMockAllRequests(shouldMockAllRequests: boolean) { | ||
| this.shouldMockAllRequests = shouldMockAllRequests; | ||
| } | ||
| public static setBaseUrl(baseUrl: string) { | ||
| this.baseUrl = baseUrl; | ||
| } | ||
| public static setDefaultHeaders(headers: Record<string, string>) { | ||
| Object.keys(headers).forEach((key: string) => { | ||
| FetchAPI.defaultHeaders[key] = headers[key]; | ||
| }); | ||
| } | ||
| public static setAuthorizationToken(token: string) { | ||
| FetchAPI.setDefaultHeaders({ Authorization: `Bearer ${token}` }); | ||
| } | ||
| public static set setDefaultRequestInfo(reqInfo: Partial<Request>) { | ||
| Object.keys(reqInfo).forEach((key: string) => { | ||
| // @ts-ignore | ||
| FetchAPI.defaultRequestInfo[key] = reqInfo[key]; | ||
| }); | ||
| } | ||
| private static serializeQueryString(params: any): string { | ||
| const urlSearchParams = Object.keys(params).map(key => { | ||
| // Encode objects and array to json. (used for cellIds and drillParams.) | ||
| const value = typeof params[key] === 'object' ? JSON.stringify(params[key]) : params[key]; | ||
| return [encodeURIComponent(key), encodeURIComponent(value)].join('='); | ||
| }); | ||
| return urlSearchParams.join('&'); | ||
| } | ||
| private static getUrl(req: RequestParams): string { | ||
| if (req.params) { | ||
| return `${this.baseUrl}${req.url}?${this.serializeQueryString(req.params)}`; | ||
| } | ||
| return `${this.baseUrl}${req.url}`; | ||
| } | ||
| private static getHeaders(req: RequestParams): Headers { | ||
| const headers = new Headers(this.defaultHeaders); | ||
| if (this.shouldMockAllRequests || req.shouldMockRequest) { | ||
| headers.set('x-mock-response', 'yes'); | ||
| } | ||
| return headers; | ||
| } | ||
| public static fetch<T = any>(req: RequestParams): Promise<T> { | ||
| const url = this.getUrl(req); | ||
| return fetch(url, { | ||
| ...this.defaultRequestInfo, | ||
| ...req, | ||
| headers: this.getHeaders(req) | ||
| }) | ||
| .then(response => { | ||
| if (response.ok) { | ||
| return response.json(); | ||
| } | ||
| return response.json().then(json => { | ||
| if (json.status_code) { | ||
| // The API provides custom response on errors. As shows in the example below. This code handles this situation by creating custom HttpError Class which allows sentry.io and AppDynamics to capture the error information. | ||
| // json example: { | ||
| // status_code: 404; | ||
| // title: "Resource Not Found" | ||
| // message: "Requested resource is unavailable, it's either been moved or deleted." | ||
| // } | ||
| return Promise.reject(new HttpError(json.status_code, response.url, json.message, json.title)); | ||
| } | ||
| return Promise.reject(new HttpError(response.status, response.url, response.statusText)); | ||
| }); | ||
| }) | ||
| .catch(error => { | ||
| throw new HttpError(error.code || 500, url, error.message || '', error.friendlyMessage); | ||
| }); | ||
| } | ||
| } |
| export const errorMessages: Record<number, string> = { | ||
| 401: 'Unauthorized, Your token has expired and needs to be renews.', | ||
| 404: 'Not Found', | ||
| 444: 'Resource Not Found', | ||
| 500: 'Sorry, something went wrong.' | ||
| }; | ||
| export default class HttpError extends Error { | ||
| public url?: string = undefined; | ||
| public code?: number = undefined; | ||
| public friendlyMessage?: string = undefined; | ||
| /** | ||
| * @param {number} httpCode | ||
| * @param {string} url | ||
| * @param {string} statusText | ||
| * @param {string} friendlyMessage | ||
| */ | ||
| constructor(httpCode: number, url: string, statusText: string, friendlyMessage?: string) { | ||
| super(); | ||
| this.name = 'HTTP Error'; | ||
| this.code = httpCode; | ||
| this.url = url; | ||
| this.message = statusText || ''; | ||
| this.friendlyMessage = friendlyMessage || errorMessages[this.code] || 'Something went wrong.'; | ||
| if (Error.captureStackTrace) { | ||
| Error.captureStackTrace(this, HttpError); | ||
| } | ||
| } | ||
| } |
| export { default } from './FetchAPI'; | ||
| export { default as HttpError } from './HttpError'; | ||
| export * from './HttpError'; | ||
| export * from './FetchAPI'; |
| { | ||
| "compilerOptions": { | ||
| /* Basic Options */ | ||
| "target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */ | ||
| "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */ | ||
| "lib": ["es2017", "es7", "es6", "dom"], /* 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. */ | ||
| // "sourceMap": true, /* Generates corresponding '.map' file. */ | ||
| // "outFile": "./", /* Concatenate and emit output to single file. */ | ||
| "outDir": "dist", /* Redirect output structure to the directory. */ | ||
| // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ | ||
| // "removeComments": true, /* Do not emit comments to output. */ | ||
| // "noEmit": true, /* 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. */ | ||
| // "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. */ | ||
| // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */ | ||
| // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */ | ||
| // "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. */ | ||
| /* 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. */ | ||
| }, | ||
| "exclude": [ | ||
| "node_modules", | ||
| "dist", | ||
| "test" | ||
| ] | ||
| } |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
4
-20%9318
-53.92%195
-53.24%