Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@minotaur-ergo/http

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@minotaur-ergo/http - npm Package Compare versions

Comparing version 0.0.12 to 0.0.13

141

dist/esm/web.js
import { WebPlugin } from '@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;
};
export class HttpWeb extends WebPlugin {
async request(options) {
console.log('ECHO', options);
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: 200,
url: options.url,
data,
headers,
status: response.status,
url: response.url,
};

@@ -11,0 +142,0 @@ }

@@ -12,10 +12,141 @@ 'use strict';

/**
* 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 HttpWeb extends core.WebPlugin {
async request(options) {
console.log('ECHO', options);
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: 200,
url: options.url,
data,
headers,
status: response.status,
url: response.url,
};

@@ -22,0 +153,0 @@ }

@@ -9,10 +9,141 @@ var capacitorHttp = (function (exports, 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 HttpWeb extends core.WebPlugin {
async request(options) {
console.log('ECHO', options);
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: 200,
url: options.url,
data,
headers,
status: response.status,
url: response.url,
};

@@ -19,0 +150,0 @@ }

2

package.json
{
"name": "@minotaur-ergo/http",
"version": "0.0.12",
"version": "0.0.13",
"description": "Cors Free Http Client",

@@ -5,0 +5,0 @@ "main": "dist/plugin.cjs.js",

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc