@vtex/api 1.9.1-beta → 1.9.3
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/CHANGELOG.md +15 -1
- package/package.json +1 -1
- package/#package.json# +0 -53
- package/lib/Apps.d.ts +0 -66
- package/lib/Apps.js +0 -211
- package/lib/Billing.d.ts +0 -18
- package/lib/Billing.js +0 -27
- package/lib/Builder.d.ts +0 -40
- package/lib/Builder.js +0 -80
- package/lib/Events.d.ts +0 -6
- package/lib/Events.js +0 -19
- package/lib/HttpClient/context.d.ts +0 -9
- package/lib/HttpClient/context.js +0 -2
- package/lib/HttpClient/index.d.ts +0 -92
- package/lib/HttpClient/index.js +0 -122
- package/lib/HttpClient/middlewares/cache.d.ts +0 -16
- package/lib/HttpClient/middlewares/cache.js +0 -86
- package/lib/HttpClient/middlewares/metrics.d.ts +0 -3
- package/lib/HttpClient/middlewares/metrics.js +0 -42
- package/lib/HttpClient/middlewares/notFound.d.ts +0 -7
- package/lib/HttpClient/middlewares/notFound.js +0 -30
- package/lib/HttpClient/middlewares/recorder.d.ts +0 -3
- package/lib/HttpClient/middlewares/recorder.js +0 -27
- package/lib/HttpClient/middlewares/request.d.ts +0 -3
- package/lib/HttpClient/middlewares/request.js +0 -41
- package/lib/ID.d.ts +0 -23
- package/lib/ID.js +0 -46
- package/lib/IODataSource.d.ts +0 -23
- package/lib/IODataSource.js +0 -43
- package/lib/Logger.d.ts +0 -20
- package/lib/Logger.js +0 -60
- package/lib/Metadata.d.ts +0 -27
- package/lib/Metadata.js +0 -53
- package/lib/Registry.d.ts +0 -52
- package/lib/Registry.js +0 -112
- package/lib/Router.d.ts +0 -35
- package/lib/Router.js +0 -52
- package/lib/VBase.d.ts +0 -40
- package/lib/VBase.js +0 -82
- package/lib/Workspaces.d.ts +0 -17
- package/lib/Workspaces.js +0 -39
- package/lib/caches/CacheLayer.d.ts +0 -7
- package/lib/caches/CacheLayer.js +0 -2
- package/lib/caches/DiskCache.d.ts +0 -17
- package/lib/caches/DiskCache.js +0 -75
- package/lib/caches/LRUCache.d.ts +0 -16
- package/lib/caches/LRUCache.js +0 -49
- package/lib/caches/LRUDiskCache.d.ts +0 -21
- package/lib/caches/LRUDiskCache.js +0 -132
- package/lib/caches/MultilayeredCache.d.ts +0 -14
- package/lib/caches/MultilayeredCache.js +0 -76
- package/lib/caches/index.d.ts +0 -6
- package/lib/caches/index.js +0 -9
- package/lib/caches/typings.d.ts +0 -48
- package/lib/caches/typings.js +0 -2
- package/lib/constants.d.ts +0 -1
- package/lib/constants.js +0 -3
- package/lib/index.d.ts +0 -19
- package/lib/index.js +0 -21
- package/lib/metrics/metricsAccumulator.d.ts +0 -27
- package/lib/metrics/metricsAccumulator.js +0 -80
- package/lib/metrics/metricsLogger.d.ts +0 -6
- package/lib/metrics/metricsLogger.js +0 -2
- package/lib/responses.d.ts +0 -95
- package/lib/responses.js +0 -2
- package/lib/runtime.d.ts +0 -27
- package/lib/runtime.js +0 -2
- package/lib/utils/AppId.d.ts +0 -19
- package/lib/utils/AppId.js +0 -42
- package/lib/utils/Time.d.ts +0 -4
- package/lib/utils/Time.js +0 -7
- package/lib/utils/index.d.ts +0 -2
- package/lib/utils/index.js +0 -7
package/lib/HttpClient/index.js
DELETED
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
-
const compose = require("koa-compose");
|
|
12
|
-
const cache_1 = require("./middlewares/cache");
|
|
13
|
-
const metrics_1 = require("./middlewares/metrics");
|
|
14
|
-
const notFound_1 = require("./middlewares/notFound");
|
|
15
|
-
const recorder_1 = require("./middlewares/recorder");
|
|
16
|
-
const request_1 = require("./middlewares/request");
|
|
17
|
-
const DEFAULT_TIMEOUT_MS = 10000;
|
|
18
|
-
const noTransforms = [(data) => data];
|
|
19
|
-
const rootURL = (service, { region }, { endpoint }) => {
|
|
20
|
-
if (endpoint) {
|
|
21
|
-
return 'http://' + endpoint;
|
|
22
|
-
}
|
|
23
|
-
if (region) {
|
|
24
|
-
return `http://${service}.${region}.vtex.io`;
|
|
25
|
-
}
|
|
26
|
-
throw new Error('Missing required: should specify either {region} or {endpoint}');
|
|
27
|
-
};
|
|
28
|
-
const workspaceURL = (service, context, opts) => {
|
|
29
|
-
const { account, workspace } = context;
|
|
30
|
-
if (!account || !workspace) {
|
|
31
|
-
throw new Error('Missing required arguments: {account, workspace}');
|
|
32
|
-
}
|
|
33
|
-
return rootURL(service, context, opts) + `/${account}/${workspace}`;
|
|
34
|
-
};
|
|
35
|
-
class HttpClient {
|
|
36
|
-
constructor(opts) {
|
|
37
|
-
this.get = (url, config = {}) => {
|
|
38
|
-
const cacheableConfig = Object.assign({}, config, { url, cacheable: true });
|
|
39
|
-
return this.request(cacheableConfig).then(response => response.data);
|
|
40
|
-
};
|
|
41
|
-
this.getRaw = (url, config = {}) => {
|
|
42
|
-
const cacheableConfig = Object.assign({}, config, { url, cacheable: true });
|
|
43
|
-
return this.request(cacheableConfig);
|
|
44
|
-
};
|
|
45
|
-
this.getBuffer = (url, config = {}) => {
|
|
46
|
-
const bufferConfig = Object.assign({}, config, { url, responseType: 'arraybuffer', transformResponse: noTransforms });
|
|
47
|
-
return this.request(bufferConfig);
|
|
48
|
-
};
|
|
49
|
-
this.getStream = (url, config = {}) => {
|
|
50
|
-
const streamConfig = Object.assign({}, config, { url, responseType: 'stream', transformResponse: noTransforms });
|
|
51
|
-
return this.request(streamConfig).then(response => response.data);
|
|
52
|
-
};
|
|
53
|
-
this.put = (url, data, config = {}) => {
|
|
54
|
-
const putConfig = Object.assign({}, config, { url, data, method: 'put' });
|
|
55
|
-
return this.request(putConfig).then(response => response.data);
|
|
56
|
-
};
|
|
57
|
-
this.post = (url, data, config = {}) => {
|
|
58
|
-
const postConfig = Object.assign({}, config, { url, data, method: 'post' });
|
|
59
|
-
return this.request(postConfig).then(response => response.data);
|
|
60
|
-
};
|
|
61
|
-
this.postRaw = (url, data, config = {}) => {
|
|
62
|
-
const postConfig = Object.assign({}, config, { url, data, method: 'post' });
|
|
63
|
-
return this.request(postConfig);
|
|
64
|
-
};
|
|
65
|
-
this.patch = (url, data, config = {}) => {
|
|
66
|
-
const patchConfig = Object.assign({}, config, { url, data, method: 'patch' });
|
|
67
|
-
return this.request(patchConfig).then(response => response.data);
|
|
68
|
-
};
|
|
69
|
-
this.delete = (url, config) => {
|
|
70
|
-
const deleteConfig = Object.assign({}, config, { url, method: 'delete' });
|
|
71
|
-
return this.request(deleteConfig);
|
|
72
|
-
};
|
|
73
|
-
this.request = (config) => __awaiter(this, void 0, void 0, function* () {
|
|
74
|
-
const context = { config };
|
|
75
|
-
yield this.runMiddlewares(context);
|
|
76
|
-
console.error('finished middlewares, returning response =', context.response);
|
|
77
|
-
return context.response;
|
|
78
|
-
});
|
|
79
|
-
const { baseURL, authToken, authType, cacheStorage, metrics, recorder, userAgent, timeout = DEFAULT_TIMEOUT_MS, segmentToken } = opts;
|
|
80
|
-
const headers = {
|
|
81
|
-
'Accept-Encoding': 'gzip',
|
|
82
|
-
'User-Agent': userAgent,
|
|
83
|
-
};
|
|
84
|
-
if (authType && authToken) {
|
|
85
|
-
headers['Authorization'] = `${authType} ${authToken}`; // tslint:disable-line
|
|
86
|
-
}
|
|
87
|
-
this.runMiddlewares = compose([
|
|
88
|
-
request_1.defaultsMiddleware(baseURL, headers, timeout),
|
|
89
|
-
...recorder ? [recorder_1.recorderMiddleware(recorder)] : [],
|
|
90
|
-
notFound_1.acceptNotFoundMiddleware,
|
|
91
|
-
...cacheStorage ? [cache_1.cacheMiddleware({ cacheStorage, segmentToken: segmentToken || '' })] : [],
|
|
92
|
-
notFound_1.notFoundFallbackMiddleware,
|
|
93
|
-
...metrics ? [metrics_1.metricsMiddleware(metrics)] : [],
|
|
94
|
-
request_1.requestMiddleware,
|
|
95
|
-
]);
|
|
96
|
-
}
|
|
97
|
-
static forWorkspace(service, context, opts) {
|
|
98
|
-
const { authToken, userAgent, recorder, segmentToken, sessionToken } = context;
|
|
99
|
-
const { timeout, cacheStorage } = opts;
|
|
100
|
-
const baseURL = workspaceURL(service, context, opts);
|
|
101
|
-
return new HttpClient({ baseURL, authType: AuthType.bearer, authToken, userAgent, timeout, recorder, cacheStorage, segmentToken, sessionToken });
|
|
102
|
-
}
|
|
103
|
-
static forRoot(service, context, opts) {
|
|
104
|
-
const { authToken, userAgent, recorder, segmentToken, sessionToken } = context;
|
|
105
|
-
const { timeout, cacheStorage } = opts;
|
|
106
|
-
const baseURL = rootURL(service, context, opts);
|
|
107
|
-
return new HttpClient({ baseURL, authType: AuthType.bearer, authToken, userAgent, timeout, recorder, cacheStorage, segmentToken, sessionToken });
|
|
108
|
-
}
|
|
109
|
-
static forLegacy(endpoint, opts) {
|
|
110
|
-
const { authToken, userAgent, timeout, cacheStorage } = opts;
|
|
111
|
-
return new HttpClient({ baseURL: endpoint, authType: AuthType.token, authToken, userAgent, timeout, cacheStorage });
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
exports.HttpClient = HttpClient;
|
|
115
|
-
exports.withoutRecorder = (ioContext) => {
|
|
116
|
-
return Object.assign({}, ioContext, { recorder: undefined });
|
|
117
|
-
};
|
|
118
|
-
var AuthType;
|
|
119
|
-
(function (AuthType) {
|
|
120
|
-
AuthType["bearer"] = "bearer";
|
|
121
|
-
AuthType["token"] = "token";
|
|
122
|
-
})(AuthType || (AuthType = {}));
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
2
|
-
import { CacheLayer } from '../../caches/CacheLayer';
|
|
3
|
-
import { MiddlewareContext } from '../context';
|
|
4
|
-
export declare const cacheMiddleware: ({ cacheStorage, segmentToken }: {
|
|
5
|
-
cacheStorage: CacheLayer<string, Cached>;
|
|
6
|
-
segmentToken: string;
|
|
7
|
-
}) => (ctx: MiddlewareContext, next: () => Promise<void>) => Promise<void>;
|
|
8
|
-
export interface Cached {
|
|
9
|
-
etag: string;
|
|
10
|
-
expiration: number;
|
|
11
|
-
response: Partial<AxiosResponse>;
|
|
12
|
-
}
|
|
13
|
-
export declare type CacheableRequestConfig = AxiosRequestConfig & {
|
|
14
|
-
url: string;
|
|
15
|
-
cacheable: boolean;
|
|
16
|
-
};
|
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
-
const url_1 = require("url");
|
|
12
|
-
const cacheKey = (config) => {
|
|
13
|
-
const { baseURL = '', url = '', params } = config;
|
|
14
|
-
const fullURL = [baseURL, url].filter(str => str).join('/');
|
|
15
|
-
const urlObject = new url_1.URL(fullURL);
|
|
16
|
-
const searchParams = new url_1.URLSearchParams(params);
|
|
17
|
-
urlObject.search = searchParams.toString();
|
|
18
|
-
return urlObject.toString();
|
|
19
|
-
};
|
|
20
|
-
const parseCacheHeaders = (headers) => {
|
|
21
|
-
const { 'cache-control': cacheControl = '', etag, age: ageStr } = headers;
|
|
22
|
-
const cacheDirectives = cacheControl.split(',').map(d => d.trim());
|
|
23
|
-
const maxAgeDirective = cacheDirectives.find(d => d.startsWith('max-age'));
|
|
24
|
-
const [, maxAgeStr] = maxAgeDirective ? maxAgeDirective.split('=') : [null, null];
|
|
25
|
-
const maxAge = maxAgeStr ? parseInt(maxAgeStr, 10) : 0;
|
|
26
|
-
const age = ageStr ? parseInt(ageStr, 10) : 0;
|
|
27
|
-
return {
|
|
28
|
-
age,
|
|
29
|
-
etag,
|
|
30
|
-
maxAge,
|
|
31
|
-
noCache: cacheDirectives.indexOf('no-cache') !== -1,
|
|
32
|
-
noStore: cacheDirectives.indexOf('no-store') !== -1,
|
|
33
|
-
};
|
|
34
|
-
};
|
|
35
|
-
function isCacheable(arg) {
|
|
36
|
-
return arg && arg.cacheable;
|
|
37
|
-
}
|
|
38
|
-
const addNotModified = (validateStatus) => (status) => validateStatus(status) || status === 304;
|
|
39
|
-
exports.cacheMiddleware = ({ cacheStorage, segmentToken }) => {
|
|
40
|
-
return (ctx, next) => __awaiter(this, void 0, void 0, function* () {
|
|
41
|
-
console.error('cacheMiddleware');
|
|
42
|
-
if (!isCacheable(ctx.config)) {
|
|
43
|
-
return yield next();
|
|
44
|
-
}
|
|
45
|
-
const key = cacheKey(ctx.config);
|
|
46
|
-
const keyWithSegment = key + segmentToken;
|
|
47
|
-
const cacheHasWithSegment = yield cacheStorage.has(keyWithSegment);
|
|
48
|
-
const cached = cacheHasWithSegment ? yield cacheStorage.get(keyWithSegment) : yield cacheStorage.get(key);
|
|
49
|
-
if (cached) {
|
|
50
|
-
const { etag: cachedEtag, response, expiration } = cached;
|
|
51
|
-
if (expiration > Date.now() && response) {
|
|
52
|
-
ctx.response = response;
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
const validateStatus = addNotModified(ctx.config.validateStatus);
|
|
56
|
-
if (cachedEtag && validateStatus(response.status)) {
|
|
57
|
-
ctx.config.headers['if-none-match'] = cachedEtag;
|
|
58
|
-
ctx.config.validateStatus = validateStatus;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
yield next();
|
|
62
|
-
if (!ctx.response) {
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
const revalidated = ctx.response.status === 304;
|
|
66
|
-
if (revalidated && cached) {
|
|
67
|
-
ctx.response = cached.response;
|
|
68
|
-
}
|
|
69
|
-
const { data, headers, status } = ctx.response;
|
|
70
|
-
const { age, etag, maxAge, noStore } = parseCacheHeaders(headers);
|
|
71
|
-
if (noStore && !etag) {
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
if (maxAge || etag) {
|
|
75
|
-
const currentAge = revalidated ? 0 : age;
|
|
76
|
-
const varySegment = ctx.response.headers.vary.includes('x-vtex-segment');
|
|
77
|
-
const setKey = varySegment ? keyWithSegment : key;
|
|
78
|
-
yield cacheStorage.set(setKey, {
|
|
79
|
-
etag,
|
|
80
|
-
expiration: Date.now() + (maxAge - currentAge) * 1000,
|
|
81
|
-
response: { data, headers, status },
|
|
82
|
-
});
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
};
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
-
const statusLabel = (status) => `${Math.floor(status / 100)}xx`;
|
|
12
|
-
exports.metricsMiddleware = (metrics) => {
|
|
13
|
-
return (ctx, next) => __awaiter(this, void 0, void 0, function* () {
|
|
14
|
-
console.error('metricsMiddleware');
|
|
15
|
-
const start = ctx.config.metric && process.hrtime();
|
|
16
|
-
const production = ctx.config.production || false;
|
|
17
|
-
let status;
|
|
18
|
-
try {
|
|
19
|
-
yield next();
|
|
20
|
-
if (ctx.response && ctx.response.status && ctx.config.metric) {
|
|
21
|
-
status = statusLabel(ctx.response.status);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
catch (err) {
|
|
25
|
-
if (err.response && err.response.status && ctx.config.metric) {
|
|
26
|
-
status = statusLabel(err.response.status);
|
|
27
|
-
}
|
|
28
|
-
else if (err.code === 'ECONNABORTED') {
|
|
29
|
-
status = 'timeout';
|
|
30
|
-
}
|
|
31
|
-
else {
|
|
32
|
-
status = 'error';
|
|
33
|
-
}
|
|
34
|
-
throw err;
|
|
35
|
-
}
|
|
36
|
-
finally {
|
|
37
|
-
if (ctx.config.metric) {
|
|
38
|
-
metrics.batchHrTimeMetric(`http-client-${ctx.config.metric}-${status}`, start, production);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
};
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { AxiosRequestConfig } from 'axios';
|
|
2
|
-
import { MiddlewareContext } from '../context';
|
|
3
|
-
export declare const acceptNotFoundMiddleware: (ctx: MiddlewareContext, next: () => Promise<void>) => Promise<void>;
|
|
4
|
-
export declare const notFoundFallbackMiddleware: (ctx: MiddlewareContext, next: () => Promise<void>) => Promise<void>;
|
|
5
|
-
export declare type IgnoreNotFoundRequestConfig = AxiosRequestConfig & {
|
|
6
|
-
nullIfNotFound?: boolean;
|
|
7
|
-
};
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
-
const addNotFound = (validateStatus) => (status) => validateStatus(status) || status === 404;
|
|
12
|
-
function nullIfNotFound(config) {
|
|
13
|
-
return config && config.nullIfNotFound;
|
|
14
|
-
}
|
|
15
|
-
exports.acceptNotFoundMiddleware = (ctx, next) => __awaiter(this, void 0, void 0, function* () {
|
|
16
|
-
console.error('acceptNotFoundMiddleware');
|
|
17
|
-
const { config } = ctx;
|
|
18
|
-
if (nullIfNotFound(config)) {
|
|
19
|
-
ctx.config.validateStatus = addNotFound(config.validateStatus);
|
|
20
|
-
}
|
|
21
|
-
yield next();
|
|
22
|
-
});
|
|
23
|
-
exports.notFoundFallbackMiddleware = (ctx, next) => __awaiter(this, void 0, void 0, function* () {
|
|
24
|
-
console.error('notFoundFallbackMiddleware');
|
|
25
|
-
yield next();
|
|
26
|
-
const { config } = ctx;
|
|
27
|
-
if (nullIfNotFound(config) && ctx.response && ctx.response.status === 404) {
|
|
28
|
-
ctx.response.data = null;
|
|
29
|
-
}
|
|
30
|
-
});
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
-
exports.recorderMiddleware = (recorder) => {
|
|
12
|
-
return (ctx, next) => __awaiter(this, void 0, void 0, function* () {
|
|
13
|
-
console.error('recorderMiddleware');
|
|
14
|
-
try {
|
|
15
|
-
yield next();
|
|
16
|
-
if (ctx.response) {
|
|
17
|
-
recorder(ctx.response.headers);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
catch (err) {
|
|
21
|
-
if (err.response && err.response.headers && err.response.status === 404) {
|
|
22
|
-
recorder(err.response.headers);
|
|
23
|
-
}
|
|
24
|
-
throw err;
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
};
|
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import { MiddlewareContext } from '../context';
|
|
2
|
-
export declare const defaultsMiddleware: (baseURL: string | undefined, headers: Record<string, string>, timeout: number) => (ctx: MiddlewareContext, next: () => Promise<void>) => Promise<void>;
|
|
3
|
-
export declare const requestMiddleware: (ctx: MiddlewareContext, next: () => Promise<void>) => Promise<void>;
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
-
const axios_1 = require("axios");
|
|
12
|
-
const retry = require("axios-retry");
|
|
13
|
-
const http_1 = require("http");
|
|
14
|
-
const http = axios_1.default.create({
|
|
15
|
-
httpAgent: new http_1.Agent({
|
|
16
|
-
keepAlive: true,
|
|
17
|
-
maxFreeSockets: 50,
|
|
18
|
-
}),
|
|
19
|
-
});
|
|
20
|
-
retry(http);
|
|
21
|
-
http.interceptors.response.use(response => response, (err) => {
|
|
22
|
-
try {
|
|
23
|
-
delete err.response.request;
|
|
24
|
-
delete err.response.config;
|
|
25
|
-
delete err.config.res;
|
|
26
|
-
delete err.config.data;
|
|
27
|
-
}
|
|
28
|
-
catch (e) { } // tslint:disable-line
|
|
29
|
-
return Promise.reject(err);
|
|
30
|
-
});
|
|
31
|
-
exports.defaultsMiddleware = (baseURL, headers, timeout) => {
|
|
32
|
-
return (ctx, next) => __awaiter(this, void 0, void 0, function* () {
|
|
33
|
-
console.error('defaultsMiddleware');
|
|
34
|
-
ctx.config = Object.assign({ baseURL, maxRedirects: 0, timeout, validateStatus: status => (status >= 200 && status < 300) }, ctx.config, { headers: Object.assign({}, headers, ctx.config.headers) });
|
|
35
|
-
yield next();
|
|
36
|
-
});
|
|
37
|
-
};
|
|
38
|
-
exports.requestMiddleware = (ctx, next) => __awaiter(this, void 0, void 0, function* () {
|
|
39
|
-
console.error('requestMiddleware');
|
|
40
|
-
ctx.response = yield http.request(ctx.config);
|
|
41
|
-
});
|
package/lib/ID.d.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { LegacyInstanceOptions } from './HttpClient';
|
|
2
|
-
export declare class ID {
|
|
3
|
-
private http;
|
|
4
|
-
private defaultHeaders;
|
|
5
|
-
constructor(endpointUrl: string | undefined, opts: LegacyInstanceOptions);
|
|
6
|
-
getTemporaryToken: () => Promise<string>;
|
|
7
|
-
sendCodeToEmail: (token: string, email: string) => Promise<any>;
|
|
8
|
-
getEmailCodeAuthenticationToken: (token: string, email: string, code: string) => Promise<AuthenticationResponse>;
|
|
9
|
-
getPasswordAuthenticationToken: (token: string, email: string, password: string) => Promise<AuthenticationResponse>;
|
|
10
|
-
}
|
|
11
|
-
export interface AuthenticationResponse {
|
|
12
|
-
promptMFA: boolean;
|
|
13
|
-
clientToken: any;
|
|
14
|
-
authCookie: {
|
|
15
|
-
Name: string;
|
|
16
|
-
Value: string;
|
|
17
|
-
};
|
|
18
|
-
accountAuthCookie: any;
|
|
19
|
-
expiresIn: number;
|
|
20
|
-
userId: string;
|
|
21
|
-
phoneNumber: string;
|
|
22
|
-
scope: any;
|
|
23
|
-
}
|
package/lib/ID.js
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const HttpClient_1 = require("./HttpClient");
|
|
4
|
-
const routes = {
|
|
5
|
-
SEND: '/accesskey/send',
|
|
6
|
-
START: '/start',
|
|
7
|
-
VALIDATE: '/accesskey/validate',
|
|
8
|
-
VALIDATE_CLASSIC: '/classic/validate',
|
|
9
|
-
};
|
|
10
|
-
const VTEXID_ENDPOINTS = {
|
|
11
|
-
BETA: 'https://vtexid.vtex.com.br/api/vtexid/pub/authentication',
|
|
12
|
-
STABLE: 'https://vtexid.vtex.com.br/api/vtexid/pub/authentication',
|
|
13
|
-
};
|
|
14
|
-
const endpoint = (env) => {
|
|
15
|
-
return VTEXID_ENDPOINTS[env] || env;
|
|
16
|
-
};
|
|
17
|
-
class ID {
|
|
18
|
-
constructor(endpointUrl = 'STABLE', opts) {
|
|
19
|
-
this.getTemporaryToken = () => {
|
|
20
|
-
return this.http.get(routes.START).then(({ authenticationToken }) => authenticationToken);
|
|
21
|
-
};
|
|
22
|
-
this.sendCodeToEmail = (token, email) => {
|
|
23
|
-
const params = { authenticationToken: token, email };
|
|
24
|
-
return this.http.get(routes.SEND, { params, headers: this.defaultHeaders });
|
|
25
|
-
};
|
|
26
|
-
this.getEmailCodeAuthenticationToken = (token, email, code) => {
|
|
27
|
-
const params = {
|
|
28
|
-
accesskey: code,
|
|
29
|
-
authenticationToken: token,
|
|
30
|
-
login: email,
|
|
31
|
-
};
|
|
32
|
-
return this.http.get(routes.VALIDATE, { params, headers: this.defaultHeaders });
|
|
33
|
-
};
|
|
34
|
-
this.getPasswordAuthenticationToken = (token, email, password) => {
|
|
35
|
-
const params = {
|
|
36
|
-
authenticationToken: token,
|
|
37
|
-
login: email,
|
|
38
|
-
password,
|
|
39
|
-
};
|
|
40
|
-
return this.http.get(routes.VALIDATE_CLASSIC, { params, headers: this.defaultHeaders });
|
|
41
|
-
};
|
|
42
|
-
this.http = HttpClient_1.HttpClient.forLegacy(endpoint(endpointUrl), opts);
|
|
43
|
-
this.defaultHeaders = opts.accept ? { accept: opts.accept } : {};
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
exports.ID = ID;
|
package/lib/IODataSource.d.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { DataSource, DataSourceConfig } from 'apollo-datasource';
|
|
2
|
-
import { HttpClient, InstanceOptions, IOContext, LegacyInstanceOptions, ServiceContext } from './HttpClient';
|
|
3
|
-
interface HttpClientFactoryOptions {
|
|
4
|
-
service: string | void;
|
|
5
|
-
context: IOContext | void;
|
|
6
|
-
options: InstanceOptions | LegacyInstanceOptions | void;
|
|
7
|
-
}
|
|
8
|
-
export declare type HttpClientFactory = (opts: HttpClientFactoryOptions) => HttpClient | void;
|
|
9
|
-
export declare abstract class IODataSource extends DataSource<ServiceContext> {
|
|
10
|
-
protected context?: IOContext | undefined;
|
|
11
|
-
private options;
|
|
12
|
-
protected abstract httpClientFactory: HttpClientFactory;
|
|
13
|
-
protected service: string | void;
|
|
14
|
-
private httpClient;
|
|
15
|
-
private initialized;
|
|
16
|
-
constructor(context?: IOContext | undefined, options?: InstanceOptions);
|
|
17
|
-
initialize(config: DataSourceConfig<ServiceContext>): void;
|
|
18
|
-
readonly http: HttpClient;
|
|
19
|
-
}
|
|
20
|
-
export declare const forWorkspace: HttpClientFactory;
|
|
21
|
-
export declare const forRoot: HttpClientFactory;
|
|
22
|
-
export declare const forLegacy: HttpClientFactory;
|
|
23
|
-
export {};
|
package/lib/IODataSource.js
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const apollo_datasource_1 = require("apollo-datasource");
|
|
4
|
-
const HttpClient_1 = require("./HttpClient");
|
|
5
|
-
class IODataSource extends apollo_datasource_1.DataSource {
|
|
6
|
-
constructor(context, options = {}) {
|
|
7
|
-
super();
|
|
8
|
-
this.context = context;
|
|
9
|
-
this.options = options;
|
|
10
|
-
this.service = undefined;
|
|
11
|
-
this.httpClient = undefined;
|
|
12
|
-
this.initialized = false;
|
|
13
|
-
}
|
|
14
|
-
initialize(config) {
|
|
15
|
-
const { context: { vtex: context }, cache: cacheStorage } = config;
|
|
16
|
-
this.context = context;
|
|
17
|
-
this.httpClient = this.httpClientFactory({
|
|
18
|
-
context,
|
|
19
|
-
options: Object.assign({ cacheStorage }, this.options),
|
|
20
|
-
service: this.service,
|
|
21
|
-
});
|
|
22
|
-
this.initialized = true;
|
|
23
|
-
}
|
|
24
|
-
get http() {
|
|
25
|
-
if (!this.initialized) {
|
|
26
|
-
this.initialize({ context: { vtex: this.context } });
|
|
27
|
-
}
|
|
28
|
-
if (this.httpClient) {
|
|
29
|
-
return this.httpClient;
|
|
30
|
-
}
|
|
31
|
-
throw new Error('IO Datasource was not initialized nor constructed with a context');
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
exports.IODataSource = IODataSource;
|
|
35
|
-
exports.forWorkspace = ({ context, service, options }) => (context && service)
|
|
36
|
-
? HttpClient_1.HttpClient.forWorkspace(service, context, options || {})
|
|
37
|
-
: undefined;
|
|
38
|
-
exports.forRoot = ({ context, service, options }) => (context && service)
|
|
39
|
-
? HttpClient_1.HttpClient.forRoot(service, context, options || {})
|
|
40
|
-
: undefined;
|
|
41
|
-
exports.forLegacy = ({ service, options }) => service
|
|
42
|
-
? HttpClient_1.HttpClient.forLegacy(service, options || {})
|
|
43
|
-
: undefined;
|
package/lib/Logger.d.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { HttpClientFactory, IODataSource } from './IODataSource';
|
|
2
|
-
export declare class Logger extends IODataSource {
|
|
3
|
-
protected service: string;
|
|
4
|
-
protected httpClientFactory: HttpClientFactory;
|
|
5
|
-
debug: (message: any, subject?: string) => Promise<void>;
|
|
6
|
-
info: (message: any, subject?: string) => Promise<void>;
|
|
7
|
-
warn: (message: any, subject?: string) => Promise<void>;
|
|
8
|
-
error: (error: any, details?: Record<string, any> | undefined, subject?: string) => Promise<void>;
|
|
9
|
-
sendLog: (subject: string, message: any, level: string) => Promise<void>;
|
|
10
|
-
}
|
|
11
|
-
export interface ErrorLog {
|
|
12
|
-
code: string;
|
|
13
|
-
message: string;
|
|
14
|
-
response?: {
|
|
15
|
-
status: number;
|
|
16
|
-
data: string;
|
|
17
|
-
headers: Record<string, string>;
|
|
18
|
-
};
|
|
19
|
-
[key: string]: any;
|
|
20
|
-
}
|
package/lib/Logger.js
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
3
|
-
var t = {};
|
|
4
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
5
|
-
t[p] = s[p];
|
|
6
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
7
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
|
|
8
|
-
t[p[i]] = s[p[i]];
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
const stringify = require("json-stringify-safe");
|
|
13
|
-
const PQueue = require("p-queue");
|
|
14
|
-
const ramda_1 = require("ramda");
|
|
15
|
-
const HttpClient_1 = require("./HttpClient");
|
|
16
|
-
const IODataSource_1 = require("./IODataSource");
|
|
17
|
-
const DEFAULT_SUBJECT = '-';
|
|
18
|
-
const PICKED_AXIOS_PROPS = ['baseURL', 'cacheable', 'data', 'finished', 'headers', 'method', 'timeout', 'status', 'path', 'url'];
|
|
19
|
-
const queue = new PQueue({ concurrency: 1 });
|
|
20
|
-
const routes = {
|
|
21
|
-
Log: (level) => `/logs/${level}`,
|
|
22
|
-
};
|
|
23
|
-
const errorReplacer = (key, value) => {
|
|
24
|
-
if (key.startsWith('_')) {
|
|
25
|
-
return undefined;
|
|
26
|
-
}
|
|
27
|
-
if (value && typeof value === 'string' && value.length > 1024) {
|
|
28
|
-
return value.substr(0, 256) + '[...TRUNCATED]';
|
|
29
|
-
}
|
|
30
|
-
return value;
|
|
31
|
-
};
|
|
32
|
-
const forWorkspaceWithoutRecorder = ({ service, context, options }) => (service && context)
|
|
33
|
-
? HttpClient_1.HttpClient.forWorkspace(service, HttpClient_1.withoutRecorder(context), options || {})
|
|
34
|
-
: undefined;
|
|
35
|
-
class Logger extends IODataSource_1.IODataSource {
|
|
36
|
-
constructor() {
|
|
37
|
-
super(...arguments);
|
|
38
|
-
this.service = 'colossus';
|
|
39
|
-
this.httpClientFactory = forWorkspaceWithoutRecorder;
|
|
40
|
-
this.debug = (message, subject = DEFAULT_SUBJECT) => this.sendLog(subject, message, 'debug');
|
|
41
|
-
this.info = (message, subject = DEFAULT_SUBJECT) => this.sendLog(subject, message, 'info');
|
|
42
|
-
this.warn = (message, subject = DEFAULT_SUBJECT) => this.sendLog(subject, message, 'warn');
|
|
43
|
-
this.error = (error, details, subject = DEFAULT_SUBJECT) => {
|
|
44
|
-
if (!error) {
|
|
45
|
-
error = new Error('Colossus.error was called with null or undefined error');
|
|
46
|
-
error.code = 'ERR_NIL_ERR';
|
|
47
|
-
console.error(error);
|
|
48
|
-
}
|
|
49
|
-
const { code: errorCode, message, stack, config, request, response } = error, rest = __rest(error, ["code", "message", "stack", "config", "request", "response"]);
|
|
50
|
-
const code = errorCode || response && `http-${response.status}`;
|
|
51
|
-
const pickedDetails = Object.assign({}, config ? { config: ramda_1.pick(PICKED_AXIOS_PROPS, config) } : undefined, request ? { request: ramda_1.pick(PICKED_AXIOS_PROPS, request) } : undefined, response ? { response: ramda_1.pick(PICKED_AXIOS_PROPS, response) } : undefined, JSON.parse(stringify(rest, errorReplacer)), details);
|
|
52
|
-
const hasDetails = Object.keys(pickedDetails).length > 0;
|
|
53
|
-
return this.sendLog(subject, { code, message, stack, details: hasDetails ? pickedDetails : undefined }, 'error');
|
|
54
|
-
};
|
|
55
|
-
this.sendLog = (subject, message, level) => {
|
|
56
|
-
return queue.add(() => this.http.put(routes.Log(level), message, { params: { subject } }));
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
exports.Logger = Logger;
|
package/lib/Metadata.d.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { InstanceOptions, IOContext } from './HttpClient';
|
|
2
|
-
import { IODataSource } from './IODataSource';
|
|
3
|
-
import { BucketMetadata } from './responses';
|
|
4
|
-
export interface MetadataEntry {
|
|
5
|
-
Key: string;
|
|
6
|
-
Hash: string;
|
|
7
|
-
Value: any;
|
|
8
|
-
}
|
|
9
|
-
export interface MetadataEntryList {
|
|
10
|
-
Data: MetadataEntry[];
|
|
11
|
-
Next: string;
|
|
12
|
-
}
|
|
13
|
-
export declare class Metadata extends IODataSource {
|
|
14
|
-
protected service: string;
|
|
15
|
-
protected httpClientFactory: import("./IODataSource").HttpClientFactory;
|
|
16
|
-
constructor(context?: IOContext, options?: InstanceOptions);
|
|
17
|
-
getBuckets: (bucket: string) => Promise<BucketMetadata>;
|
|
18
|
-
list: (bucket: string, includeValue: boolean, limit?: number | undefined, nextMarker?: string | undefined) => Promise<MetadataEntryList>;
|
|
19
|
-
listAll: (bucket: string, includeValue: boolean) => Promise<MetadataEntryList>;
|
|
20
|
-
get: (bucket: string, key: string) => Promise<any>;
|
|
21
|
-
save: (bucket: string, key: string, data: any) => Promise<void>;
|
|
22
|
-
saveAll: (bucket: string, data: {
|
|
23
|
-
[key: string]: any;
|
|
24
|
-
}) => Promise<void>;
|
|
25
|
-
delete: (bucket: string, key: string) => Promise<import("./HttpClient").IOResponse<void>>;
|
|
26
|
-
deleteAll: (bucket: string) => Promise<import("./HttpClient").IOResponse<void>>;
|
|
27
|
-
}
|