@rushdb/javascript-sdk 0.9.3 → 0.9.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/{types → dist}/api/create-api.d.ts +6 -2
- package/dist/index.cjs +1 -0
- package/dist/index.mjs +1 -0
- package/dist/index.node.d.ts +137 -0
- package/dist/index.worker.d.ts +137 -0
- package/{types → dist}/sdk/model.d.ts +4 -4
- package/{types → dist}/sdk/record.d.ts +14 -12
- package/{types → dist}/sdk/sdk.d.ts +24 -24
- package/{types → dist}/sdk/transaction.d.ts +2 -2
- package/{types → dist}/types/query.d.ts +3 -3
- package/package.json +10 -34
- package/CHANGELOG.md +0 -174
- package/cjs/api/api.js +0 -328
- package/cjs/api/constants.js +0 -7
- package/cjs/api/create-api.js +0 -313
- package/cjs/api/index.js +0 -8
- package/cjs/api/rest-api-proxy.js +0 -22
- package/cjs/api/types.js +0 -2
- package/cjs/api/utils.js +0 -155
- package/cjs/common/constants.js +0 -10
- package/cjs/common/utils.js +0 -46
- package/cjs/index.cjs.node.js +0 -24
- package/cjs/index.cjs.worker.js +0 -24
- package/cjs/network/FetchHttpClient.js +0 -138
- package/cjs/network/HttpClient.js +0 -59
- package/cjs/network/NodeHttpClient.js +0 -103
- package/cjs/network/index.js +0 -28
- package/cjs/network/types.js +0 -2
- package/cjs/package.json +0 -4
- package/cjs/sdk/constants.js +0 -13
- package/cjs/sdk/errors.js +0 -27
- package/cjs/sdk/index.js +0 -23
- package/cjs/sdk/model.js +0 -147
- package/cjs/sdk/record.js +0 -84
- package/cjs/sdk/sdk.js +0 -46
- package/cjs/sdk/transaction.js +0 -17
- package/cjs/sdk/types.js +0 -2
- package/cjs/sdk/utils.js +0 -103
- package/cjs/types/expressions.js +0 -2
- package/cjs/types/index.js +0 -21
- package/cjs/types/query.js +0 -2
- package/cjs/types/schema.js +0 -2
- package/cjs/types/utils.js +0 -2
- package/cjs/types/value.js +0 -2
- package/esm/api/api.js +0 -324
- package/esm/api/constants.js +0 -4
- package/esm/api/create-api.js +0 -309
- package/esm/api/index.js +0 -2
- package/esm/api/rest-api-proxy.js +0 -18
- package/esm/api/types.js +0 -1
- package/esm/api/utils.js +0 -139
- package/esm/common/constants.js +0 -7
- package/esm/common/utils.js +0 -37
- package/esm/index.esm.node.js +0 -6
- package/esm/index.esm.worker.js +0 -6
- package/esm/network/FetchHttpClient.js +0 -133
- package/esm/network/HttpClient.js +0 -53
- package/esm/network/NodeHttpClient.js +0 -98
- package/esm/network/index.js +0 -24
- package/esm/network/types.js +0 -1
- package/esm/package.json +0 -4
- package/esm/sdk/constants.js +0 -10
- package/esm/sdk/errors.js +0 -21
- package/esm/sdk/index.js +0 -7
- package/esm/sdk/model.js +0 -143
- package/esm/sdk/record.js +0 -77
- package/esm/sdk/sdk.js +0 -42
- package/esm/sdk/transaction.js +0 -13
- package/esm/sdk/types.js +0 -1
- package/esm/sdk/utils.js +0 -93
- package/esm/types/expressions.js +0 -1
- package/esm/types/index.js +0 -5
- package/esm/types/query.js +0 -1
- package/esm/types/schema.js +0 -1
- package/esm/types/utils.js +0 -1
- package/esm/types/value.js +0 -1
- package/types/index.d.ts +0 -60
- /package/{types → dist}/api/api.d.ts +0 -0
- /package/{types → dist}/api/constants.d.ts +0 -0
- /package/{types → dist}/api/index.d.ts +0 -0
- /package/{types → dist}/api/rest-api-proxy.d.ts +0 -0
- /package/{types → dist}/api/types.d.ts +0 -0
- /package/{types → dist}/api/utils.d.ts +0 -0
- /package/{types → dist}/common/constants.d.ts +0 -0
- /package/{types → dist}/common/utils.d.ts +0 -0
- /package/{types → dist}/network/FetchHttpClient.d.ts +0 -0
- /package/{types → dist}/network/HttpClient.d.ts +0 -0
- /package/{types → dist}/network/NodeHttpClient.d.ts +0 -0
- /package/{types → dist}/network/index.d.ts +0 -0
- /package/{types → dist}/network/types.d.ts +0 -0
- /package/{types → dist}/sdk/constants.d.ts +0 -0
- /package/{types → dist}/sdk/errors.d.ts +0 -0
- /package/{types → dist}/sdk/index.d.ts +0 -0
- /package/{types → dist}/sdk/types.d.ts +0 -0
- /package/{types → dist}/sdk/utils.d.ts +0 -0
- /package/{types → dist}/types/expressions.d.ts +0 -0
- /package/{types → dist}/types/index.d.ts +0 -0
- /package/{types → dist}/types/schema.d.ts +0 -0
- /package/{types → dist}/types/utils.d.ts +0 -0
- /package/{types → dist}/types/value.d.ts +0 -0
package/esm/api/utils.js
DELETED
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
import { ISO_8601_FULL, PROPERTY_TYPE_BOOLEAN, PROPERTY_TYPE_DATETIME, PROPERTY_TYPE_NULL, PROPERTY_TYPE_NUMBER, PROPERTY_TYPE_STRING } from '../common/constants.js';
|
|
2
|
-
import { isArray, isObject, isString } from '../common/utils.js';
|
|
3
|
-
import { DBRecordInstance } from '../sdk';
|
|
4
|
-
import { Transaction } from '../sdk/transaction.js';
|
|
5
|
-
import { DEFAULT_BASE_PATH, DEFAULT_HOST, DEFAULT_PORT, DEFAULT_PROTOCOL } from './constants.js';
|
|
6
|
-
export const buildTransactionHeader = (txId) => txId ?
|
|
7
|
-
{
|
|
8
|
-
['x-transaction-id']: txId
|
|
9
|
-
}
|
|
10
|
-
: undefined;
|
|
11
|
-
export const isTransaction = (input) => isString(input) || input instanceof Transaction;
|
|
12
|
-
export const pickTransaction = (input) => (isTransaction(input) ? input : undefined);
|
|
13
|
-
export const pickTransactionId = (input) => isTransaction(input) ?
|
|
14
|
-
input instanceof Transaction ?
|
|
15
|
-
input.id
|
|
16
|
-
: input
|
|
17
|
-
: undefined;
|
|
18
|
-
export const pickRecordId = (input) => {
|
|
19
|
-
if (isString(input)) {
|
|
20
|
-
return input;
|
|
21
|
-
}
|
|
22
|
-
else if (input instanceof DBRecordInstance && input.data) {
|
|
23
|
-
return input.data.__id;
|
|
24
|
-
}
|
|
25
|
-
else if ('__id' in input) {
|
|
26
|
-
return input.__id;
|
|
27
|
-
}
|
|
28
|
-
return undefined;
|
|
29
|
-
};
|
|
30
|
-
export const createSearchParams = (labelOrSearchParams, searchParamsOrTransaction) => {
|
|
31
|
-
var _a, _b, _c;
|
|
32
|
-
const isFirstArgString = isString(labelOrSearchParams);
|
|
33
|
-
const isFirstArgUUID = isUUID(labelOrSearchParams);
|
|
34
|
-
const isSecondArgTransaction = isTransaction(searchParamsOrTransaction);
|
|
35
|
-
const isEmptySearchParams = isSecondArgTransaction || !isObject(searchParamsOrTransaction);
|
|
36
|
-
if (isFirstArgString) {
|
|
37
|
-
const baseParams = isFirstArgUUID ?
|
|
38
|
-
{ id: labelOrSearchParams }
|
|
39
|
-
: { searchParams: { labels: [labelOrSearchParams] } };
|
|
40
|
-
return isEmptySearchParams ? Object.assign(Object.assign({}, baseParams), { searchParams: Object.assign({}, baseParams.searchParams) }) : Object.assign(Object.assign({}, baseParams), { searchParams: Object.assign(Object.assign({}, searchParamsOrTransaction), { labels: [
|
|
41
|
-
...((_b = (_a = baseParams.searchParams) === null || _a === void 0 ? void 0 : _a.labels) !== null && _b !== void 0 ? _b : []),
|
|
42
|
-
...((_c = searchParamsOrTransaction.labels) !== null && _c !== void 0 ? _c : [])
|
|
43
|
-
] }) });
|
|
44
|
-
}
|
|
45
|
-
else {
|
|
46
|
-
return { searchParams: labelOrSearchParams !== null && labelOrSearchParams !== void 0 ? labelOrSearchParams : {} };
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
export const isUUID = (value) => {
|
|
50
|
-
const regex = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/;
|
|
51
|
-
return regex.test(value);
|
|
52
|
-
};
|
|
53
|
-
export const arrayIsConsistent = (arr) => arr.every((item) => typeof item === typeof arr[0]);
|
|
54
|
-
export const getValueParameters = (value) => {
|
|
55
|
-
if (Array.isArray(value)) {
|
|
56
|
-
return {
|
|
57
|
-
isEmptyArray: value.length === 0,
|
|
58
|
-
isEmptyStringsArray: value.every((v) => v === ''),
|
|
59
|
-
isInconsistentArray: !arrayIsConsistent(value)
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
else {
|
|
63
|
-
return { isEmptyString: value === '' };
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
export const suggestPropertyType = (value) => {
|
|
67
|
-
if (typeof value === PROPERTY_TYPE_STRING) {
|
|
68
|
-
return ISO_8601_FULL.test(value) ? PROPERTY_TYPE_DATETIME : PROPERTY_TYPE_STRING;
|
|
69
|
-
}
|
|
70
|
-
else if (typeof value === PROPERTY_TYPE_NUMBER) {
|
|
71
|
-
return PROPERTY_TYPE_NUMBER;
|
|
72
|
-
}
|
|
73
|
-
else if (typeof value === PROPERTY_TYPE_BOOLEAN) {
|
|
74
|
-
return PROPERTY_TYPE_BOOLEAN;
|
|
75
|
-
}
|
|
76
|
-
else if (value === null) {
|
|
77
|
-
return PROPERTY_TYPE_NULL;
|
|
78
|
-
}
|
|
79
|
-
else {
|
|
80
|
-
return PROPERTY_TYPE_STRING;
|
|
81
|
-
}
|
|
82
|
-
};
|
|
83
|
-
const processArrayValue = (value, suggestTypes) => {
|
|
84
|
-
const { isEmptyArray, isInconsistentArray } = getValueParameters(value);
|
|
85
|
-
if (isEmptyArray) {
|
|
86
|
-
return { type: PROPERTY_TYPE_STRING, value: [] };
|
|
87
|
-
}
|
|
88
|
-
if (isInconsistentArray || !suggestTypes) {
|
|
89
|
-
return { type: PROPERTY_TYPE_STRING, value: value.map(String) };
|
|
90
|
-
}
|
|
91
|
-
return { type: suggestPropertyType(value[0]), value };
|
|
92
|
-
};
|
|
93
|
-
const processNonArrayValue = (value, suggestTypes) => {
|
|
94
|
-
if (!suggestTypes) {
|
|
95
|
-
return { type: PROPERTY_TYPE_STRING, value: String(value) };
|
|
96
|
-
}
|
|
97
|
-
const type = suggestPropertyType(value);
|
|
98
|
-
return { type, value: type === PROPERTY_TYPE_NULL ? null : value };
|
|
99
|
-
};
|
|
100
|
-
export const prepareProperties = (data, options = { suggestTypes: true }) => Object.entries(data).map(([name, value]) => {
|
|
101
|
-
const { type, value: processedValue } = isArray(value) ?
|
|
102
|
-
processArrayValue(value, options.suggestTypes)
|
|
103
|
-
: processNonArrayValue(value, options.suggestTypes);
|
|
104
|
-
return { name, type, value: processedValue };
|
|
105
|
-
});
|
|
106
|
-
export const normalizeRecord = ({ label, options = { suggestTypes: true }, payload }) => ({
|
|
107
|
-
label,
|
|
108
|
-
properties: prepareProperties(payload, options)
|
|
109
|
-
});
|
|
110
|
-
export const buildUrl = (props) => {
|
|
111
|
-
let protocol = DEFAULT_PROTOCOL;
|
|
112
|
-
let host = DEFAULT_HOST;
|
|
113
|
-
let port = DEFAULT_PORT;
|
|
114
|
-
let basePath = DEFAULT_BASE_PATH;
|
|
115
|
-
if ('url' in props) {
|
|
116
|
-
const url = new URL(props.url);
|
|
117
|
-
protocol = url.protocol.replace(':', '');
|
|
118
|
-
host = url.hostname;
|
|
119
|
-
port = parseInt(url.port ||
|
|
120
|
-
(protocol === 'http' ? '80'
|
|
121
|
-
: protocol === 'https' ? '443'
|
|
122
|
-
: ''));
|
|
123
|
-
}
|
|
124
|
-
if ('host' in props && 'port' in props && 'protocol' in props) {
|
|
125
|
-
protocol = props.protocol.replace(':', '');
|
|
126
|
-
host = props.host;
|
|
127
|
-
port = props.port;
|
|
128
|
-
}
|
|
129
|
-
// Ensure the basePath starts with a '/'
|
|
130
|
-
if (basePath && !basePath.startsWith('/')) {
|
|
131
|
-
basePath = '/' + basePath;
|
|
132
|
-
}
|
|
133
|
-
// If the port is the default for the protocol (80 for http, 443 for https), it can be omitted
|
|
134
|
-
let portString = '';
|
|
135
|
-
if (!((protocol === 'http' && port === 80) || (protocol === 'https' && port === 443))) {
|
|
136
|
-
portString = ':' + port;
|
|
137
|
-
}
|
|
138
|
-
return `${protocol}://${host}${portString}${basePath}`;
|
|
139
|
-
};
|
package/esm/common/constants.js
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export const ISO_8601_FULL = /^(?:\d{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1\d|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:\d{2}(?:[02468][048]|[13579][26])-02-29))Schema(0[0-9]|1[0-9]|2[0-3]):(0[0-9]|[1-5][0-9]):(0[0-9]|[1-5][0-9])?(?:\.([0-9]{1,9}))?([zZ]?|([\+-])(((([0][0-9])|([1][0-3])):?(([03][0])|([14][5])))|14:00)?)$/;
|
|
2
|
-
export const DEFAULT_TIMEOUT = 80000;
|
|
3
|
-
export const PROPERTY_TYPE_STRING = 'string';
|
|
4
|
-
export const PROPERTY_TYPE_DATETIME = 'datetime';
|
|
5
|
-
export const PROPERTY_TYPE_BOOLEAN = 'boolean';
|
|
6
|
-
export const PROPERTY_TYPE_NUMBER = 'number';
|
|
7
|
-
export const PROPERTY_TYPE_NULL = 'null';
|
package/esm/common/utils.js
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
export const isArray = (item) => typeof item === 'object' && Array.isArray(item) && item !== null;
|
|
2
|
-
export const isObject = (input) => input !== null && Object.prototype.toString.call(input) === '[object Object]';
|
|
3
|
-
export const isEmptyObject = (input) => isObject(input) && Object.keys(input).length === 0;
|
|
4
|
-
export const isObjectFlat = (input) => {
|
|
5
|
-
return (isObject(input) &&
|
|
6
|
-
Object.keys(input).every((key) => {
|
|
7
|
-
const value = input[key];
|
|
8
|
-
// Check if the value is an array
|
|
9
|
-
if (isArray(value)) {
|
|
10
|
-
// Check if every element in the array is of an allowed type (string, number, boolean, or null)
|
|
11
|
-
return value.every((element) => typeof element === 'string' ||
|
|
12
|
-
typeof element === 'number' ||
|
|
13
|
-
typeof element === 'boolean' ||
|
|
14
|
-
element === null);
|
|
15
|
-
}
|
|
16
|
-
// Check if the value is a non-null object (excluding arrays, which are also typeof 'object')
|
|
17
|
-
return !(value && typeof value === 'object');
|
|
18
|
-
}));
|
|
19
|
-
};
|
|
20
|
-
export const isString = (input) => typeof input === 'string';
|
|
21
|
-
export const toBoolean = (value) => {
|
|
22
|
-
if (typeof value === 'string') {
|
|
23
|
-
if (value.toLowerCase() === 'true') {
|
|
24
|
-
return true;
|
|
25
|
-
}
|
|
26
|
-
else if (value.toLowerCase() === 'false') {
|
|
27
|
-
return false;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
if (isArray(value) && value.length === 0) {
|
|
31
|
-
return false;
|
|
32
|
-
}
|
|
33
|
-
if (isObject(value) && Object.keys(value).length === 0) {
|
|
34
|
-
return false;
|
|
35
|
-
}
|
|
36
|
-
return Boolean(value);
|
|
37
|
-
};
|
package/esm/index.esm.node.js
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { HttpClient, HttpClientResponse } from './network/HttpClient.js';
|
|
2
|
-
import { NodeHttpClient } from './network/NodeHttpClient.js';
|
|
3
|
-
import { DBRecordsBatchDraft, Model, DBRecordInstance, DBRecordsArrayInstance, Transaction, EmptyTargetError, NonUniqueResultError, UniquenessError, initSDK, idToDate, idToTimestamp } from './sdk/index.js';
|
|
4
|
-
const RushDB = initSDK(new NodeHttpClient());
|
|
5
|
-
export { RushDB, DBRecordsBatchDraft, Model, DBRecordInstance, DBRecordsArrayInstance, Transaction, EmptyTargetError, HttpClient, HttpClientResponse, NonUniqueResultError, UniquenessError, idToDate, idToTimestamp };
|
|
6
|
-
export default RushDB;
|
package/esm/index.esm.worker.js
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { FetchHttpClient } from './network/FetchHttpClient.js';
|
|
2
|
-
import { HttpClient, HttpClientResponse } from './network/HttpClient.js';
|
|
3
|
-
import { DBRecordsBatchDraft, Model, DBRecordInstance, DBRecordsArrayInstance, Transaction, EmptyTargetError, NonUniqueResultError, UniquenessError, initSDK, idToDate, idToTimestamp } from './sdk/index.js';
|
|
4
|
-
const RushDB = initSDK(new FetchHttpClient());
|
|
5
|
-
export { RushDB, DBRecordsBatchDraft, Model, DBRecordInstance, DBRecordsArrayInstance, Transaction, EmptyTargetError, HttpClient, HttpClientResponse, NonUniqueResultError, UniquenessError, idToDate, idToTimestamp };
|
|
6
|
-
export default RushDB;
|
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
import { DEFAULT_TIMEOUT } from '../common/constants.js';
|
|
2
|
-
import { HttpClient, HttpClientGenericResponse } from './HttpClient.js';
|
|
3
|
-
/**
|
|
4
|
-
* HTTP client which uses a `fetch` function to issue requests.
|
|
5
|
-
*
|
|
6
|
-
* By default, relies on the global `fetch` function, but an optional function
|
|
7
|
-
* can be passed in. If passing in a function, it is expected to match the Web
|
|
8
|
-
* Fetch API. As an example, this could be the function provided by the
|
|
9
|
-
* node-fetch package (https://github.com/node-fetch/node-fetch).
|
|
10
|
-
*/
|
|
11
|
-
export class FetchHttpClient extends HttpClient {
|
|
12
|
-
constructor(fetchFn) {
|
|
13
|
-
super();
|
|
14
|
-
// Default to global fetch if available
|
|
15
|
-
if (!fetchFn) {
|
|
16
|
-
if (!globalThis.fetch) {
|
|
17
|
-
throw new Error('fetch() function not provided and is not defined in the global scope. ' +
|
|
18
|
-
'You must provide a fetch implementation.');
|
|
19
|
-
}
|
|
20
|
-
fetchFn = globalThis.fetch;
|
|
21
|
-
}
|
|
22
|
-
// Both timeout behaviors differs from Node:
|
|
23
|
-
// - Fetch uses a single timeout for the entire length of the request.
|
|
24
|
-
// - Node is more fine-grained and resets the timeout after each stage of the request.
|
|
25
|
-
if (globalThis.AbortController) {
|
|
26
|
-
// Utilise native AbortController if available
|
|
27
|
-
// AbortController was added in Node v15.0.0, v14.17.0
|
|
28
|
-
this._fetchFn = FetchHttpClient.makeFetchWithAbortTimeout(fetchFn);
|
|
29
|
-
}
|
|
30
|
-
else {
|
|
31
|
-
// Fall back to racing against a timeout promise if not available in the runtime
|
|
32
|
-
// This does not actually cancel the underlying fetch operation or resources
|
|
33
|
-
this._fetchFn = FetchHttpClient.makeFetchWithRaceTimeout(fetchFn);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
static makeFetchWithRaceTimeout(fetchFn) {
|
|
37
|
-
return (url, init, timeout) => {
|
|
38
|
-
let pendingTimeoutId;
|
|
39
|
-
const timeoutPromise = new Promise((_, reject) => {
|
|
40
|
-
pendingTimeoutId = setTimeout(() => {
|
|
41
|
-
pendingTimeoutId = null;
|
|
42
|
-
reject(HttpClient.makeTimeoutError());
|
|
43
|
-
}, timeout);
|
|
44
|
-
});
|
|
45
|
-
const fetchPromise = fetchFn(url, init);
|
|
46
|
-
return Promise.race([fetchPromise, timeoutPromise]).finally(() => {
|
|
47
|
-
if (pendingTimeoutId) {
|
|
48
|
-
clearTimeout(pendingTimeoutId);
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
static makeFetchWithAbortTimeout(fetchFn) {
|
|
54
|
-
return async (url, init, timeout) => {
|
|
55
|
-
// Use AbortController because AbortSignal.timeout() was added later in Node v17.3.0, v16.14.0
|
|
56
|
-
const abort = new AbortController();
|
|
57
|
-
let timeoutId = setTimeout(() => {
|
|
58
|
-
timeoutId = null;
|
|
59
|
-
abort.abort(HttpClient.makeTimeoutError());
|
|
60
|
-
}, timeout);
|
|
61
|
-
try {
|
|
62
|
-
return await fetchFn(url, Object.assign(Object.assign({}, init), { signal: abort.signal }));
|
|
63
|
-
}
|
|
64
|
-
catch (err) {
|
|
65
|
-
// Some implementations, like node-fetch, do not respect the reason passed to AbortController.abort()
|
|
66
|
-
// and instead it always throws an AbortError
|
|
67
|
-
// We catch this case to normalise all timeout errors
|
|
68
|
-
if (err.name === 'AbortError') {
|
|
69
|
-
throw HttpClient.makeTimeoutError();
|
|
70
|
-
}
|
|
71
|
-
else {
|
|
72
|
-
throw err;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
finally {
|
|
76
|
-
if (timeoutId) {
|
|
77
|
-
clearTimeout(timeoutId);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
async makeRequest(url, { headers, method, protocol, requestData, timeout }) {
|
|
83
|
-
// For methods which expect payloads, we should always pass a body value
|
|
84
|
-
// even when it is empty. Without this, some JS runtimes (eg. Deno) will
|
|
85
|
-
// inject a second Content-Length header.
|
|
86
|
-
const methodHasPayload = method == 'POST' || method == 'PUT' || method == 'PATCH';
|
|
87
|
-
const body = JSON.stringify(requestData) || (methodHasPayload ? '' : undefined);
|
|
88
|
-
const res = await this._fetchFn(url, {
|
|
89
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
90
|
-
// @ts-ignore
|
|
91
|
-
body,
|
|
92
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
93
|
-
// @ts-ignore
|
|
94
|
-
headers,
|
|
95
|
-
method
|
|
96
|
-
}, timeout !== null && timeout !== void 0 ? timeout : DEFAULT_TIMEOUT);
|
|
97
|
-
return new FetchHttpClientResponse(res);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
export class FetchHttpClientResponse extends HttpClientGenericResponse {
|
|
101
|
-
constructor(res) {
|
|
102
|
-
super(res.status, FetchHttpClientResponse._transformHeadersToObject(res.headers));
|
|
103
|
-
this._res = res;
|
|
104
|
-
}
|
|
105
|
-
getRawResponse() {
|
|
106
|
-
return this._res;
|
|
107
|
-
}
|
|
108
|
-
toStream(streamCompleteCallback) {
|
|
109
|
-
// Unfortunately `fetch` does not have event handlers for when the stream is
|
|
110
|
-
// completely read. We therefore invoke the streamCompleteCallback right
|
|
111
|
-
// away. This callback emits a response event with metadata and completes
|
|
112
|
-
// metrics, so it's ok to do this without waiting for the stream to be
|
|
113
|
-
// completely read.
|
|
114
|
-
streamCompleteCallback();
|
|
115
|
-
// Fetch's `body` property is expected to be a readable stream of the body.
|
|
116
|
-
return this._res.body;
|
|
117
|
-
}
|
|
118
|
-
toJSON() {
|
|
119
|
-
return this._res.json();
|
|
120
|
-
}
|
|
121
|
-
static _transformHeadersToObject(headers) {
|
|
122
|
-
// Fetch uses a Headers instance so this must be converted to a barebones
|
|
123
|
-
// JS object to meet the HttpClient interface.
|
|
124
|
-
const headersObj = {};
|
|
125
|
-
for (const entry of headers) {
|
|
126
|
-
if (!Array.isArray(entry) || entry.length != 2) {
|
|
127
|
-
throw new Error('Response objects produced by the fetch function given to FetchHttpClient do not have an iterable headers map. Response#headers should be an iterable object.');
|
|
128
|
-
}
|
|
129
|
-
headersObj[entry[0]] = entry[1];
|
|
130
|
-
}
|
|
131
|
-
return headersObj;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
export class HttpClient {
|
|
2
|
-
makeRequest(url, { headers, method, protocol, requestData, timeout }) {
|
|
3
|
-
throw new Error('makeRequest not implemented.');
|
|
4
|
-
}
|
|
5
|
-
static makeTimeoutError() {
|
|
6
|
-
const timeoutErr = new TypeError(HttpClient.TIMEOUT_ERROR_CODE);
|
|
7
|
-
timeoutErr.code = HttpClient.TIMEOUT_ERROR_CODE;
|
|
8
|
-
return timeoutErr;
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
HttpClient.CONNECTION_CLOSED_ERROR_CODES = ['ECONNRESET', 'EPIPE'];
|
|
12
|
-
HttpClient.TIMEOUT_ERROR_CODE = 'ETIMEDOUT';
|
|
13
|
-
export class HttpClientGenericResponse {
|
|
14
|
-
constructor(statusCode, headers) {
|
|
15
|
-
this._statusCode = statusCode;
|
|
16
|
-
this._headers = headers;
|
|
17
|
-
}
|
|
18
|
-
getStatusCode() {
|
|
19
|
-
return this._statusCode;
|
|
20
|
-
}
|
|
21
|
-
getHeaders() {
|
|
22
|
-
return this._headers;
|
|
23
|
-
}
|
|
24
|
-
getRawResponse() {
|
|
25
|
-
throw new Error('getRawResponse not implemented.');
|
|
26
|
-
}
|
|
27
|
-
toStream(streamCompleteCallback) {
|
|
28
|
-
throw new Error('toStream not implemented.');
|
|
29
|
-
}
|
|
30
|
-
toJSON() {
|
|
31
|
-
throw new Error('toJSON not implemented.');
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
export class HttpClientResponse {
|
|
35
|
-
constructor(response) {
|
|
36
|
-
this._res = response;
|
|
37
|
-
}
|
|
38
|
-
getStatusCode() {
|
|
39
|
-
throw new Error('getStatusCode not implemented.');
|
|
40
|
-
}
|
|
41
|
-
getHeaders() {
|
|
42
|
-
throw new Error('getHeaders not implemented.');
|
|
43
|
-
}
|
|
44
|
-
getRawResponse() {
|
|
45
|
-
throw new Error('getRawResponse not implemented.');
|
|
46
|
-
}
|
|
47
|
-
toStream(streamCompleteCallback) {
|
|
48
|
-
throw new Error('toStream not implemented.');
|
|
49
|
-
}
|
|
50
|
-
toJSON() {
|
|
51
|
-
throw new Error('toJSON not implemented.');
|
|
52
|
-
}
|
|
53
|
-
}
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import * as http_ from 'http';
|
|
2
|
-
import * as https_ from 'https';
|
|
3
|
-
import { DEFAULT_TIMEOUT } from '../common/constants.js';
|
|
4
|
-
import { HttpClient, HttpClientGenericResponse } from './HttpClient.js';
|
|
5
|
-
// `import * as http_ from 'http'` creates a "Module Namespace Exotic Object"
|
|
6
|
-
// which is immune to monkey-patching, whereas http_.default (in an ES Module context)
|
|
7
|
-
// will resolve to the same thing as require('http'), which is
|
|
8
|
-
// monkey-patchable. We care about this because users in their test
|
|
9
|
-
// suites might be using a library like "nock" which relies on the ability
|
|
10
|
-
// to monkey-patch and intercept calls to http.request.
|
|
11
|
-
const http = http_.default || http_;
|
|
12
|
-
const https = https_.default || https_;
|
|
13
|
-
const defaultHttpAgent = new http.Agent({ keepAlive: true });
|
|
14
|
-
const defaultHttpsAgent = new https.Agent({ keepAlive: true });
|
|
15
|
-
export class NodeHttpClient extends HttpClient {
|
|
16
|
-
constructor(agent) {
|
|
17
|
-
super();
|
|
18
|
-
this._agent = agent;
|
|
19
|
-
}
|
|
20
|
-
makeRequest(urlString, { headers, method, protocol, requestData, timeout }) {
|
|
21
|
-
const url = new URL(urlString);
|
|
22
|
-
const isInsecureConnection = protocol === 'http' || url.protocol === 'http:';
|
|
23
|
-
let agent = this._agent;
|
|
24
|
-
if (!agent) {
|
|
25
|
-
agent = isInsecureConnection ? defaultHttpAgent : defaultHttpsAgent;
|
|
26
|
-
}
|
|
27
|
-
return new Promise((resolve, reject) => {
|
|
28
|
-
const req = (isInsecureConnection ? http : https).request({
|
|
29
|
-
agent,
|
|
30
|
-
ciphers: 'DEFAULT:!aNULL:!eNULL:!LOW:!EXPORT:!SSLv2:!MD5',
|
|
31
|
-
headers,
|
|
32
|
-
host: url.hostname,
|
|
33
|
-
method,
|
|
34
|
-
path: url.pathname + url.search,
|
|
35
|
-
port: url.port
|
|
36
|
-
});
|
|
37
|
-
req.setTimeout(timeout !== null && timeout !== void 0 ? timeout : DEFAULT_TIMEOUT, () => {
|
|
38
|
-
req.destroy(HttpClient.makeTimeoutError());
|
|
39
|
-
});
|
|
40
|
-
req.on('response', (res) => {
|
|
41
|
-
resolve(new NodeHttpClientResponse(res));
|
|
42
|
-
});
|
|
43
|
-
req.on('error', (error) => {
|
|
44
|
-
reject(error);
|
|
45
|
-
});
|
|
46
|
-
req.once('socket', (socket) => {
|
|
47
|
-
const payload = requestData ? JSON.stringify(requestData) : '';
|
|
48
|
-
if (socket.connecting) {
|
|
49
|
-
socket.once(isInsecureConnection ? 'connect' : 'secureConnect', () => {
|
|
50
|
-
// Send payload; we're safe:
|
|
51
|
-
req.write(payload);
|
|
52
|
-
req.end();
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
else {
|
|
56
|
-
// we're already connected
|
|
57
|
-
req.write(payload);
|
|
58
|
-
req.end();
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
export class NodeHttpClientResponse extends HttpClientGenericResponse {
|
|
65
|
-
constructor(res) {
|
|
66
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
67
|
-
// @ts-ignore
|
|
68
|
-
super(res.statusCode, res.headers || {});
|
|
69
|
-
this._res = res;
|
|
70
|
-
}
|
|
71
|
-
getRawResponse() {
|
|
72
|
-
return this._res;
|
|
73
|
-
}
|
|
74
|
-
toStream(streamCompleteCallback) {
|
|
75
|
-
// The raw response is itself the stream, so we just return that. To be
|
|
76
|
-
// backwards compatible, we should invoke the streamCompleteCallback only
|
|
77
|
-
// once the stream has been fully consumed.
|
|
78
|
-
this._res.once('end', () => streamCompleteCallback());
|
|
79
|
-
return this._res;
|
|
80
|
-
}
|
|
81
|
-
toJSON() {
|
|
82
|
-
return new Promise((resolve, reject) => {
|
|
83
|
-
let response = '';
|
|
84
|
-
this._res.setEncoding('utf8');
|
|
85
|
-
this._res.on('data', (chunk) => {
|
|
86
|
-
response += chunk;
|
|
87
|
-
});
|
|
88
|
-
this._res.once('end', () => {
|
|
89
|
-
try {
|
|
90
|
-
resolve(JSON.parse(response));
|
|
91
|
-
}
|
|
92
|
-
catch (e) {
|
|
93
|
-
reject(e);
|
|
94
|
-
}
|
|
95
|
-
});
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
}
|
package/esm/network/index.js
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
2
|
-
var t = {};
|
|
3
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
4
|
-
t[p] = s[p];
|
|
5
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
6
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
7
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
8
|
-
t[p[i]] = s[p[i]];
|
|
9
|
-
}
|
|
10
|
-
return t;
|
|
11
|
-
};
|
|
12
|
-
const defaultHeaders = {
|
|
13
|
-
'Content-Type': 'application/json'
|
|
14
|
-
};
|
|
15
|
-
export const createFetcher = ({ httpClient, token, url }) => async (input, _a) => {
|
|
16
|
-
var { headers: initHeaders } = _a, init = __rest(_a, ["headers"]);
|
|
17
|
-
const response = await httpClient.makeRequest(`${url}${input}`, Object.assign({ credentials: 'omit', headers: Object.assign(Object.assign(Object.assign({}, defaultHeaders), initHeaders), typeof token !== 'undefined' ? { token } : {}) }, init));
|
|
18
|
-
if (response.getStatusCode() >= 200 && response.getStatusCode() < 300) {
|
|
19
|
-
const data = await response.toJSON();
|
|
20
|
-
return data;
|
|
21
|
-
}
|
|
22
|
-
// @TODO: Make it more useful
|
|
23
|
-
throw new Error(`${response.getStatusCode()}`);
|
|
24
|
-
};
|
package/esm/network/types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/esm/package.json
DELETED
package/esm/sdk/constants.js
DELETED
package/esm/sdk/errors.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
export class UniquenessError extends Error {
|
|
2
|
-
constructor(label, properties) {
|
|
3
|
-
super(`Record with label "${label}" and properties ${JSON.stringify(properties)} already exists`);
|
|
4
|
-
this.name = 'UniquenessError';
|
|
5
|
-
Object.setPrototypeOf(this, UniquenessError.prototype);
|
|
6
|
-
}
|
|
7
|
-
}
|
|
8
|
-
export class EmptyTargetError extends Error {
|
|
9
|
-
constructor(message) {
|
|
10
|
-
super(message);
|
|
11
|
-
this.name = 'EmptyTarget';
|
|
12
|
-
Object.setPrototypeOf(this, EmptyTargetError.prototype);
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
export class NonUniqueResultError extends Error {
|
|
16
|
-
constructor(duplicateCount, searchParams) {
|
|
17
|
-
super(`Expected a unique result but found ${duplicateCount} matches for the provided search parameters: ${JSON.stringify(searchParams)}. Ensure your search parameters uniquely identify a single result.`);
|
|
18
|
-
this.name = 'NonUniqueResultError';
|
|
19
|
-
Object.setPrototypeOf(this, NonUniqueResultError.prototype);
|
|
20
|
-
}
|
|
21
|
-
}
|