@minotaur-ergo/http
Advanced tools
Comparing version 0.0.10 to 0.0.11
@@ -10,3 +10,3 @@ { | ||
"name": "request", | ||
"signature": "(options: { value: string; }) => Promise<{ value: string; }>", | ||
"signature": "(options: HttpOptions) => Promise<HttpResponse>", | ||
"parameters": [ | ||
@@ -16,9 +16,12 @@ { | ||
"docs": "", | ||
"type": "{ value: string; }" | ||
"type": "HttpOptions" | ||
} | ||
], | ||
"returns": "Promise<{ value: string; }>", | ||
"returns": "Promise<HttpResponse>", | ||
"tags": [], | ||
"docs": "", | ||
"complexTypes": [], | ||
"complexTypes": [ | ||
"HttpResponse", | ||
"HttpOptions" | ||
], | ||
"slug": "request" | ||
@@ -29,6 +32,191 @@ } | ||
}, | ||
"interfaces": [], | ||
"interfaces": [ | ||
{ | ||
"name": "HttpResponse", | ||
"slug": "httpresponse", | ||
"docs": "", | ||
"tags": [], | ||
"methods": [], | ||
"properties": [ | ||
{ | ||
"name": "data", | ||
"tags": [], | ||
"docs": "", | ||
"complexTypes": [], | ||
"type": "any" | ||
}, | ||
{ | ||
"name": "status", | ||
"tags": [], | ||
"docs": "", | ||
"complexTypes": [], | ||
"type": "number" | ||
}, | ||
{ | ||
"name": "headers", | ||
"tags": [], | ||
"docs": "", | ||
"complexTypes": [ | ||
"HttpHeaders" | ||
], | ||
"type": "HttpHeaders" | ||
}, | ||
{ | ||
"name": "url", | ||
"tags": [], | ||
"docs": "", | ||
"complexTypes": [], | ||
"type": "string" | ||
} | ||
] | ||
}, | ||
{ | ||
"name": "HttpHeaders", | ||
"slug": "httpheaders", | ||
"docs": "", | ||
"tags": [], | ||
"methods": [], | ||
"properties": [] | ||
}, | ||
{ | ||
"name": "HttpOptions", | ||
"slug": "httpoptions", | ||
"docs": "", | ||
"tags": [], | ||
"methods": [], | ||
"properties": [ | ||
{ | ||
"name": "url", | ||
"tags": [], | ||
"docs": "", | ||
"complexTypes": [], | ||
"type": "string" | ||
}, | ||
{ | ||
"name": "method", | ||
"tags": [], | ||
"docs": "", | ||
"complexTypes": [], | ||
"type": "string | undefined" | ||
}, | ||
{ | ||
"name": "params", | ||
"tags": [], | ||
"docs": "", | ||
"complexTypes": [ | ||
"HttpParams" | ||
], | ||
"type": "HttpParams" | ||
}, | ||
{ | ||
"name": "data", | ||
"tags": [], | ||
"docs": "Note: On Android and iOS, data can only be a string or a JSON.\nFormData, Blob, ArrayBuffer, and other complex types are only directly supported on web\nor through enabling `CapacitorHttp` in the config and using the patched `window.fetch` or `XMLHttpRequest`.\n\nIf you need to send a complex type, you should serialize the data to base64\nand set the `headers[\"Content-Type\"]` and `dataType` attributes accordingly.", | ||
"complexTypes": [], | ||
"type": "any" | ||
}, | ||
{ | ||
"name": "headers", | ||
"tags": [], | ||
"docs": "", | ||
"complexTypes": [ | ||
"HttpHeaders" | ||
], | ||
"type": "HttpHeaders" | ||
}, | ||
{ | ||
"name": "readTimeout", | ||
"tags": [], | ||
"docs": "How long to wait to read additional data. Resets each time new\ndata is received", | ||
"complexTypes": [], | ||
"type": "number | undefined" | ||
}, | ||
{ | ||
"name": "connectTimeout", | ||
"tags": [], | ||
"docs": "How long to wait for the initial connection.", | ||
"complexTypes": [], | ||
"type": "number | undefined" | ||
}, | ||
{ | ||
"name": "disableRedirects", | ||
"tags": [], | ||
"docs": "Sets whether automatic HTTP redirects should be disabled", | ||
"complexTypes": [], | ||
"type": "boolean | undefined" | ||
}, | ||
{ | ||
"name": "webFetchExtra", | ||
"tags": [], | ||
"docs": "Extra arguments for fetch when running on the web", | ||
"complexTypes": [ | ||
"RequestInit" | ||
], | ||
"type": "RequestInit" | ||
}, | ||
{ | ||
"name": "responseType", | ||
"tags": [], | ||
"docs": "This is used to parse the response appropriately before returning it to\nthe requestee. If the response content-type is \"json\", this value is ignored.", | ||
"complexTypes": [ | ||
"HttpResponseType" | ||
], | ||
"type": "HttpResponseType" | ||
}, | ||
{ | ||
"name": "shouldEncodeUrlParams", | ||
"tags": [], | ||
"docs": "Use this option if you need to keep the URL unencoded in certain cases\n(already encoded, azure/firebase testing, etc.). The default is _true_.", | ||
"complexTypes": [], | ||
"type": "boolean | undefined" | ||
}, | ||
{ | ||
"name": "dataType", | ||
"tags": [], | ||
"docs": "This is used if we've had to convert the data from a JS type that needs\nspecial handling in the native layer", | ||
"complexTypes": [], | ||
"type": "'file' | 'formData' | undefined" | ||
} | ||
] | ||
}, | ||
{ | ||
"name": "HttpParams", | ||
"slug": "httpparams", | ||
"docs": "", | ||
"tags": [], | ||
"methods": [], | ||
"properties": [] | ||
} | ||
], | ||
"enums": [], | ||
"typeAliases": [], | ||
"typeAliases": [ | ||
{ | ||
"name": "HttpResponseType", | ||
"slug": "httpresponsetype", | ||
"docs": "", | ||
"types": [ | ||
{ | ||
"text": "'arraybuffer'", | ||
"complexTypes": [] | ||
}, | ||
{ | ||
"text": "'blob'", | ||
"complexTypes": [] | ||
}, | ||
{ | ||
"text": "'json'", | ||
"complexTypes": [] | ||
}, | ||
{ | ||
"text": "'text'", | ||
"complexTypes": [] | ||
}, | ||
{ | ||
"text": "'document'", | ||
"complexTypes": [] | ||
} | ||
] | ||
} | ||
], | ||
"pluginConfigs": [] | ||
} |
export interface HttpPlugin { | ||
request(options: { | ||
value: string; | ||
}): Promise<{ | ||
value: string; | ||
}>; | ||
request(options: HttpOptions): Promise<HttpResponse>; | ||
} | ||
declare type HttpResponseType = 'arraybuffer' | 'blob' | 'json' | 'text' | 'document'; | ||
export interface HttpOptions { | ||
url: string; | ||
method?: string; | ||
params?: HttpParams; | ||
data?: any; | ||
headers?: HttpHeaders; | ||
/** | ||
* How long to wait to read additional data. Resets each time new | ||
* data is received | ||
*/ | ||
readTimeout?: number; | ||
/** | ||
* How long to wait for the initial connection. | ||
*/ | ||
connectTimeout?: number; | ||
/** | ||
* Sets whether automatic HTTP redirects should be disabled | ||
*/ | ||
disableRedirects?: boolean; | ||
/** | ||
* Extra arguments for fetch when running on the web | ||
*/ | ||
webFetchExtra?: RequestInit; | ||
/** | ||
* This is used to parse the response appropriately before returning it to | ||
* the requestee. If the response content-type is "json", this value is ignored. | ||
*/ | ||
responseType?: HttpResponseType; | ||
/** | ||
* Use this option if you need to keep the URL unencoded in certain cases | ||
* (already encoded, azure/firebase testing, etc.). The default is _true_. | ||
*/ | ||
shouldEncodeUrlParams?: boolean; | ||
} | ||
export interface HttpParams { | ||
[key: string]: string | string[]; | ||
} | ||
export interface HttpHeaders { | ||
[key: string]: string; | ||
} | ||
export interface HttpResponse { | ||
data: any; | ||
status: number; | ||
headers: HttpHeaders; | ||
url: string; | ||
} | ||
export {}; |
import { registerPlugin } from '@capacitor/core'; | ||
const Http = registerPlugin('Http', { | ||
web: () => import('./web').then(m => new m.HttpWeb()), | ||
electron: () => window.CapacitorCustomPlatform.plugins.Http | ||
electron: () => window.CapacitorCustomPlatform.plugins.Http, | ||
}); | ||
@@ -6,0 +6,0 @@ export * from './definitions'; |
import { WebPlugin } from '@capacitor/core'; | ||
import type { HttpPlugin } from './definitions'; | ||
import type { HttpPlugin, HttpResponse, HttpOptions } from './definitions'; | ||
export declare class HttpWeb extends WebPlugin implements HttpPlugin { | ||
request(options: { | ||
value: string; | ||
}): Promise<{ | ||
value: string; | ||
}>; | ||
request(options: HttpOptions): Promise<HttpResponse>; | ||
} |
@@ -5,5 +5,10 @@ import { WebPlugin } from '@capacitor/core'; | ||
console.log('ECHO', options); | ||
return options; | ||
return { | ||
data: '', | ||
headers: {}, | ||
status: 200, | ||
url: options.url, | ||
}; | ||
} | ||
} | ||
//# sourceMappingURL=web.js.map |
@@ -9,3 +9,3 @@ 'use strict'; | ||
web: () => Promise.resolve().then(function () { return web; }).then(m => new m.HttpWeb()), | ||
electron: () => window.CapacitorCustomPlatform.plugins.Http | ||
electron: () => window.CapacitorCustomPlatform.plugins.Http, | ||
}); | ||
@@ -16,3 +16,8 @@ | ||
console.log('ECHO', options); | ||
return options; | ||
return { | ||
data: '', | ||
headers: {}, | ||
status: 200, | ||
url: options.url, | ||
}; | ||
} | ||
@@ -19,0 +24,0 @@ } |
@@ -6,3 +6,3 @@ var capacitorHttp = (function (exports, core) { | ||
web: () => Promise.resolve().then(function () { return web; }).then(m => new m.HttpWeb()), | ||
electron: () => window.CapacitorCustomPlatform.plugins.Http | ||
electron: () => window.CapacitorCustomPlatform.plugins.Http, | ||
}); | ||
@@ -13,3 +13,8 @@ | ||
console.log('ECHO', options); | ||
return options; | ||
return { | ||
data: '', | ||
headers: {}, | ||
status: 200, | ||
url: options.url, | ||
}; | ||
} | ||
@@ -16,0 +21,0 @@ } |
@@ -6,6 +6,142 @@ 'use strict'; | ||
const core_1 = require("@capacitor/core"); | ||
/** | ||
* Read in a Blob value and return it as a base64 string | ||
* @param blob The blob value to convert to a base64 string | ||
*/ | ||
const readBlobAsBase64 = async (blob) => new Promise((resolve, reject) => { | ||
const reader = new FileReader(); | ||
reader.onload = () => { | ||
const base64String = reader.result; | ||
const base64StringWithoutTags = base64String.substr(base64String.indexOf(',') + 1); // remove prefix "data:application/pdf;base64," | ||
resolve(base64StringWithoutTags); | ||
}; | ||
reader.onerror = (error) => reject(error); | ||
reader.readAsDataURL(blob); | ||
}); | ||
/** | ||
* Builds a string of url parameters that | ||
* @param params A map of url parameters | ||
* @param shouldEncode true if you should encodeURIComponent() the values (true by default) | ||
*/ | ||
const buildUrlParams = (params, shouldEncode = true) => { | ||
if (!params) | ||
return null; | ||
const output = Object.entries(params).reduce((accumulator, entry) => { | ||
const [key, value] = entry; | ||
let encodedValue; | ||
let item; | ||
if (Array.isArray(value)) { | ||
item = ''; | ||
value.forEach(str => { | ||
encodedValue = shouldEncode ? encodeURIComponent(str) : str; | ||
item += `${key}=${encodedValue}&`; | ||
}); | ||
// last character will always be "&" so slice it off | ||
item.slice(0, -1); | ||
} | ||
else { | ||
encodedValue = shouldEncode ? encodeURIComponent(value) : value; | ||
item = `${key}=${encodedValue}`; | ||
} | ||
return `${accumulator}&${item}`; | ||
}, ''); | ||
// Remove initial "&" from the reduce | ||
return output.substr(1); | ||
}; | ||
/** | ||
* Normalize an HttpHeaders map by lowercasing all of the values | ||
* @param headers The HttpHeaders object to normalize | ||
*/ | ||
const normalizeHttpHeaders = (headers = {}) => { | ||
const originalKeys = Object.keys(headers); | ||
const loweredKeys = Object.keys(headers).map(k => k.toLocaleLowerCase()); | ||
const normalized = loweredKeys.reduce((acc, key, index) => { | ||
acc[key] = headers[originalKeys[index]]; | ||
return acc; | ||
}, {}); | ||
return normalized; | ||
}; | ||
/** | ||
* Build the RequestInit object based on the options passed into the initial request | ||
* @param options The Http plugin options | ||
* @param extra Any extra RequestInit values | ||
*/ | ||
const buildRequestInit = (options, extra = {}) => { | ||
const output = Object.assign({ method: options.method || 'GET', headers: options.headers }, extra); | ||
// Get the content-type | ||
const headers = normalizeHttpHeaders(options.headers); | ||
const type = headers['content-type'] || ''; | ||
// If body is already a string, then pass it through as-is. | ||
if (typeof options.data === 'string') { | ||
output.body = options.data; | ||
} | ||
// Build request initializers based off of content-type | ||
else if (type.includes('application/x-www-form-urlencoded')) { | ||
const params = new URLSearchParams(); | ||
for (const [key, value] of Object.entries(options.data || {})) { | ||
params.set(key, value); | ||
} | ||
output.body = params.toString(); | ||
} | ||
else if (type.includes('multipart/form-data')) { | ||
const form = new FormData(); | ||
if (options.data instanceof FormData) { | ||
options.data.forEach((value, key) => { | ||
form.append(key, value); | ||
}); | ||
} | ||
else { | ||
for (const key of Object.keys(options.data)) { | ||
form.append(key, options.data[key]); | ||
} | ||
} | ||
output.body = form; | ||
const headers = new Headers(output.headers); | ||
headers.delete('content-type'); // content-type will be set by `window.fetch` to includy boundary | ||
output.headers = headers; | ||
} | ||
else if (type.includes('application/json') || | ||
typeof options.data === 'object') { | ||
output.body = JSON.stringify(options.data); | ||
} | ||
return output; | ||
}; | ||
class Http extends core_1.WebPlugin { | ||
async request(options) { | ||
console.log('ECHO', options); | ||
return { value: "changed in electron" }; | ||
const requestInit = buildRequestInit(options, options.webFetchExtra); | ||
const urlParams = buildUrlParams(options.params, options.shouldEncodeUrlParams); | ||
const url = urlParams ? `${options.url}?${urlParams}` : options.url; | ||
const response = await fetch(url, requestInit); | ||
const contentType = response.headers.get('content-type') || ''; | ||
// Default to 'text' responseType so no parsing happens | ||
let { responseType = 'text' } = response.ok ? options : {}; | ||
// If the response content-type is json, force the response to be json | ||
if (contentType.includes('application/json')) { | ||
responseType = 'json'; | ||
} | ||
let data; | ||
switch (responseType) { | ||
case 'arraybuffer': | ||
case 'blob': | ||
data = await readBlobAsBase64(await response.blob()); | ||
break; | ||
case 'json': | ||
data = await response.json(); | ||
break; | ||
case 'document': | ||
case 'text': | ||
default: | ||
data = await response.text(); | ||
} | ||
// Convert fetch headers to Capacitor HttpHeaders | ||
const headers = {}; | ||
response.headers.forEach((value, key) => { | ||
headers[key] = value; | ||
}); | ||
return { | ||
data, | ||
headers, | ||
status: response.status, | ||
url: response.url, | ||
}; | ||
} | ||
@@ -12,0 +148,0 @@ } |
@@ -12,2 +12,2 @@ export default { | ||
external: ['@capacitor/core'], | ||
}; | ||
}; |
{ | ||
"compilerOptions": { | ||
"allowSyntheticDefaultImports": true, | ||
"declaration": true, | ||
"experimentalDecorators": true, | ||
"noEmitHelpers": true, | ||
"importHelpers": true, | ||
"lib": ["dom", "es2020"], | ||
"module": "commonjs", | ||
"noImplicitAny": true, | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true, | ||
"outDir": "build", | ||
"sourceMap": true, | ||
"strict": false, | ||
"target": "ES2017" | ||
}, | ||
"include": ["src/**/*"] | ||
} | ||
"compilerOptions": { | ||
"allowSyntheticDefaultImports": true, | ||
"declaration": true, | ||
"experimentalDecorators": true, | ||
"noEmitHelpers": true, | ||
"importHelpers": true, | ||
"lib": ["dom", "es2020"], | ||
"module": "commonjs", | ||
"noImplicitAny": true, | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true, | ||
"outDir": "build", | ||
"sourceMap": true, | ||
"strict": false, | ||
"target": "ES2017" | ||
}, | ||
"include": ["src/**/*"] | ||
} |
{ | ||
"name": "@minotaur-ergo/http", | ||
"version": "0.0.10", | ||
"version": "0.0.11", | ||
"description": "Cors Free Http Client", | ||
@@ -36,2 +36,3 @@ "main": "dist/plugin.cjs.js", | ||
"verify:web": "npm run build", | ||
"prettify": "prettier --ignore-unknown --write .", | ||
"lint": "npm run eslint && npm run prettier -- --check && npm run swiftlint -- lint", | ||
@@ -38,0 +39,0 @@ "fmt": "npm run eslint -- --fix && npm run prettier -- --write && npm run swiftlint -- --fix --format", |
@@ -17,2 +17,4 @@ # @minotaur-ergo/http | ||
* [`request(...)`](#request) | ||
* [Interfaces](#interfaces) | ||
* [Type Aliases](#type-aliases) | ||
@@ -27,13 +29,58 @@ </docgen-index> | ||
```typescript | ||
request(options: { value: string; }) => Promise<{ value: string; }> | ||
request(options: HttpOptions) => Promise<HttpResponse> | ||
``` | ||
| Param | Type | | ||
| ------------- | ------------------------------- | | ||
| **`options`** | <code>{ value: string; }</code> | | ||
| Param | Type | | ||
| ------------- | --------------------------------------------------- | | ||
| **`options`** | <code><a href="#httpoptions">HttpOptions</a></code> | | ||
**Returns:** <code>Promise<{ value: string; }></code> | ||
**Returns:** <code>Promise<<a href="#httpresponse">HttpResponse</a>></code> | ||
-------------------- | ||
### Interfaces | ||
#### HttpResponse | ||
| Prop | Type | | ||
| ------------- | --------------------------------------------------- | | ||
| **`data`** | <code>any</code> | | ||
| **`status`** | <code>number</code> | | ||
| **`headers`** | <code><a href="#httpheaders">HttpHeaders</a></code> | | ||
| **`url`** | <code>string</code> | | ||
#### HttpHeaders | ||
#### HttpOptions | ||
| Prop | Type | Description | | ||
| --------------------------- | ------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| **`url`** | <code>string</code> | | | ||
| **`method`** | <code>string</code> | | | ||
| **`params`** | <code><a href="#httpparams">HttpParams</a></code> | | | ||
| **`data`** | <code>any</code> | Note: On Android and iOS, data can only be a string or a JSON. FormData, Blob, ArrayBuffer, and other complex types are only directly supported on web or through enabling `CapacitorHttp` in the config and using the patched `window.fetch` or `XMLHttpRequest`. If you need to send a complex type, you should serialize the data to base64 and set the `headers["Content-Type"]` and `dataType` attributes accordingly. | | ||
| **`headers`** | <code><a href="#httpheaders">HttpHeaders</a></code> | | | ||
| **`readTimeout`** | <code>number</code> | How long to wait to read additional data. Resets each time new data is received | | ||
| **`connectTimeout`** | <code>number</code> | How long to wait for the initial connection. | | ||
| **`disableRedirects`** | <code>boolean</code> | Sets whether automatic HTTP redirects should be disabled | | ||
| **`webFetchExtra`** | <code>RequestInit</code> | Extra arguments for fetch when running on the web | | ||
| **`responseType`** | <code><a href="#httpresponsetype">HttpResponseType</a></code> | This is used to parse the response appropriately before returning it to the requestee. If the response content-type is "json", this value is ignored. | | ||
| **`shouldEncodeUrlParams`** | <code>boolean</code> | Use this option if you need to keep the URL unencoded in certain cases (already encoded, azure/firebase testing, etc.). The default is _true_. | | ||
| **`dataType`** | <code>'file' \| 'formData'</code> | This is used if we've had to convert the data from a JS type that needs special handling in the native layer | | ||
#### HttpParams | ||
### Type Aliases | ||
#### HttpResponseType | ||
<code>'arraybuffer' | 'blob' | 'json' | 'text' | 'document'</code> | ||
</docgen-api> |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
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
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
49690
528
85
2