@forge/cache 0.9.0 → 0.10.0-next.0
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/out/__test__/cache.test.js +20 -12
- package/out/cache.d.ts +6 -0
- package/out/cache.d.ts.map +1 -1
- package/out/cache.js +23 -2
- package/package.json +1 -1
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const tslib_1 = require("tslib");
|
|
4
4
|
const node_fetch_1 = tslib_1.__importDefault(require("node-fetch"));
|
|
5
|
-
const api_1 = require("@forge/api");
|
|
6
5
|
const cache_1 = require("../cache");
|
|
7
6
|
jest.mock('node-fetch');
|
|
8
7
|
const fetchMock = node_fetch_1.default;
|
|
@@ -106,9 +105,18 @@ describe('Cache', () => {
|
|
|
106
105
|
it('handles failure', async () => {
|
|
107
106
|
const { cache, fetch } = buildCache();
|
|
108
107
|
fetch.mockResolvedValueOnce({ ok: false, text: async () => 'Not allowed', status: 403 });
|
|
109
|
-
await expect(cache.set('key', 'value', { ttlSeconds: 100 })).rejects.toMatchError(new
|
|
108
|
+
await expect(cache.set('key', 'value', { ttlSeconds: 100 })).rejects.toMatchError(new cache_1.ApiError(403, 'UNKNOWN_ERROR', 'Not allowed'));
|
|
110
109
|
expect(fetch.mock.lastCall).toMatchSnapshot();
|
|
111
110
|
});
|
|
111
|
+
it('handles failure with error code', async () => {
|
|
112
|
+
const { cache, fetch } = buildCache();
|
|
113
|
+
fetch.mockResolvedValueOnce({
|
|
114
|
+
ok: false,
|
|
115
|
+
text: async () => JSON.stringify({ error: { code: 'NOT_ALLOWED', title: 'Invalid operation' } }),
|
|
116
|
+
status: 400
|
|
117
|
+
});
|
|
118
|
+
await expect(cache.set('key', 'value', { ttlSeconds: 100 })).rejects.toMatchError(new cache_1.ApiError(400, 'NOT_ALLOWED', '"title":"Invalid operation"'));
|
|
119
|
+
});
|
|
112
120
|
});
|
|
113
121
|
describe('setIfNotExists', () => {
|
|
114
122
|
it('handles success', async () => {
|
|
@@ -134,7 +142,7 @@ describe('Cache', () => {
|
|
|
134
142
|
it('handles failure', async () => {
|
|
135
143
|
const { cache, fetch } = buildCache();
|
|
136
144
|
fetch.mockResolvedValueOnce({ ok: false, text: async () => 'Not allowed', status: 403 });
|
|
137
|
-
await expect(cache.setIfNotExists('key', 'value', { ttlSeconds: 100 })).rejects.toMatchError(new
|
|
145
|
+
await expect(cache.setIfNotExists('key', 'value', { ttlSeconds: 100 })).rejects.toMatchError(new cache_1.ApiError(403, 'UNKNOWN_ERROR', 'Not allowed'));
|
|
138
146
|
expect(fetch.mock.lastCall).toMatchSnapshot();
|
|
139
147
|
});
|
|
140
148
|
});
|
|
@@ -160,7 +168,7 @@ describe('Cache', () => {
|
|
|
160
168
|
it('handles failure', async () => {
|
|
161
169
|
const { cache, fetch } = buildCache();
|
|
162
170
|
fetch.mockResolvedValueOnce({ ok: false, text: async () => 'Not allowed', status: 403 });
|
|
163
|
-
await expect(cache.get('key')).rejects.toMatchError(new
|
|
171
|
+
await expect(cache.get('key')).rejects.toMatchError(new cache_1.ApiError(403, 'UNKNOWN_ERROR', 'Not allowed'));
|
|
164
172
|
expect(fetch.mock.lastCall).toMatchSnapshot();
|
|
165
173
|
});
|
|
166
174
|
});
|
|
@@ -196,7 +204,7 @@ describe('Cache', () => {
|
|
|
196
204
|
it('handles failure', async () => {
|
|
197
205
|
const { cache, fetch } = buildCache();
|
|
198
206
|
fetch.mockResolvedValueOnce({ ok: false, text: async () => 'Not allowed', status: 403 });
|
|
199
|
-
await expect(cache.getAndSet('key', 'value', { ttlSeconds: 100 })).rejects.toMatchError(new
|
|
207
|
+
await expect(cache.getAndSet('key', 'value', { ttlSeconds: 100 })).rejects.toMatchError(new cache_1.ApiError(403, 'UNKNOWN_ERROR', 'Not allowed'));
|
|
200
208
|
expect(fetch.mock.lastCall).toMatchSnapshot();
|
|
201
209
|
});
|
|
202
210
|
});
|
|
@@ -225,7 +233,7 @@ describe('Cache', () => {
|
|
|
225
233
|
it('handles failure', async () => {
|
|
226
234
|
const { cache, fetch } = buildCache();
|
|
227
235
|
fetch.mockResolvedValueOnce({ ok: false, text: async () => 'Not allowed', status: 403 });
|
|
228
|
-
await expect(cache.incrementAndGet('key')).rejects.toMatchError(new
|
|
236
|
+
await expect(cache.incrementAndGet('key')).rejects.toMatchError(new cache_1.ApiError(403, 'UNKNOWN_ERROR', 'Not allowed'));
|
|
229
237
|
expect(fetch.mock.lastCall).toMatchSnapshot();
|
|
230
238
|
});
|
|
231
239
|
});
|
|
@@ -254,7 +262,7 @@ describe('Cache', () => {
|
|
|
254
262
|
it('handles failure', async () => {
|
|
255
263
|
const { cache, fetch } = buildCache();
|
|
256
264
|
fetch.mockResolvedValueOnce({ ok: false, text: async () => 'Not allowed', status: 403 });
|
|
257
|
-
await expect(cache.decrementAndGet('key')).rejects.toMatchError(new
|
|
265
|
+
await expect(cache.decrementAndGet('key')).rejects.toMatchError(new cache_1.ApiError(403, 'UNKNOWN_ERROR', 'Not allowed'));
|
|
258
266
|
expect(fetch.mock.lastCall).toMatchSnapshot();
|
|
259
267
|
});
|
|
260
268
|
});
|
|
@@ -269,7 +277,7 @@ describe('Cache', () => {
|
|
|
269
277
|
it('handles failure', async () => {
|
|
270
278
|
const { cache, fetch } = buildCache();
|
|
271
279
|
fetch.mockResolvedValueOnce({ ok: false, text: async () => 'Not allowed', status: 403 });
|
|
272
|
-
await expect(cache.delete('key')).rejects.toMatchError(new
|
|
280
|
+
await expect(cache.delete('key')).rejects.toMatchError(new cache_1.ApiError(403, 'UNKNOWN_ERROR', 'Not allowed'));
|
|
273
281
|
expect(fetch.mock.lastCall).toMatchSnapshot();
|
|
274
282
|
});
|
|
275
283
|
});
|
|
@@ -301,7 +309,7 @@ describe('Cache', () => {
|
|
|
301
309
|
it('handles failure', async () => {
|
|
302
310
|
const { cache, fetch } = buildCache();
|
|
303
311
|
fetch.mockResolvedValueOnce({ ok: false, text: async () => 'Not allowed', status: 403 });
|
|
304
|
-
await expect(cache.scan('key*', { cursor: '0', count: 2 })).rejects.toMatchError(new
|
|
312
|
+
await expect(cache.scan('key*', { cursor: '0', count: 2 })).rejects.toMatchError(new cache_1.ApiError(403, 'UNKNOWN_ERROR', 'Not allowed'));
|
|
305
313
|
expect(fetch.mock.lastCall).toMatchSnapshot();
|
|
306
314
|
});
|
|
307
315
|
});
|
|
@@ -316,7 +324,7 @@ describe('Cache', () => {
|
|
|
316
324
|
it('handles failure', async () => {
|
|
317
325
|
const { cache, fetch } = buildCache();
|
|
318
326
|
fetch.mockResolvedValueOnce({ ok: false, text: async () => 'Not allowed', status: 403 });
|
|
319
|
-
await expect(cache.leftPush('list', 'value')).rejects.toMatchError(new
|
|
327
|
+
await expect(cache.leftPush('list', 'value')).rejects.toMatchError(new cache_1.ApiError(403, 'UNKNOWN_ERROR', 'Not allowed'));
|
|
320
328
|
expect(fetch.mock.lastCall).toMatchSnapshot();
|
|
321
329
|
});
|
|
322
330
|
});
|
|
@@ -338,7 +346,7 @@ describe('Cache', () => {
|
|
|
338
346
|
it('handles failure', async () => {
|
|
339
347
|
const { cache, fetch } = buildCache();
|
|
340
348
|
fetch.mockResolvedValueOnce({ ok: false, text: async () => 'Not allowed', status: 403 });
|
|
341
|
-
await expect(cache.rightPop('list')).rejects.toMatchError(new
|
|
349
|
+
await expect(cache.rightPop('list')).rejects.toMatchError(new cache_1.ApiError(403, 'UNKNOWN_ERROR', 'Not allowed'));
|
|
342
350
|
expect(fetch.mock.lastCall).toMatchSnapshot();
|
|
343
351
|
});
|
|
344
352
|
});
|
|
@@ -360,7 +368,7 @@ describe('Cache', () => {
|
|
|
360
368
|
it('handles failure', async () => {
|
|
361
369
|
const { cache, fetch } = buildCache();
|
|
362
370
|
fetch.mockResolvedValueOnce({ ok: false, text: async () => 'Not allowed', status: 403 });
|
|
363
|
-
await expect(cache.listLength('list')).rejects.toMatchError(new
|
|
371
|
+
await expect(cache.listLength('list')).rejects.toMatchError(new cache_1.ApiError(403, 'UNKNOWN_ERROR', 'Not allowed'));
|
|
364
372
|
expect(fetch.mock.lastCall).toMatchSnapshot();
|
|
365
373
|
});
|
|
366
374
|
});
|
package/out/cache.d.ts
CHANGED
|
@@ -6,6 +6,12 @@ export declare type ScanResult = {
|
|
|
6
6
|
cursor: string;
|
|
7
7
|
keys: string[];
|
|
8
8
|
};
|
|
9
|
+
export declare class ApiError extends Error {
|
|
10
|
+
readonly status: number;
|
|
11
|
+
readonly code: string;
|
|
12
|
+
readonly data?: any;
|
|
13
|
+
constructor(status: number, code: string, message: string, data?: any);
|
|
14
|
+
}
|
|
9
15
|
export declare function getResponseBody(response: Response): Promise<any>;
|
|
10
16
|
export declare class Cache implements IForgeCache {
|
|
11
17
|
private client;
|
package/out/cache.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../src/cache.ts"],"names":[],"mappings":"AACA,OAAkB,EAAE,WAAW,EAAE,QAAQ,IAAI,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAEnF,OAAO,EAAE,WAAW,EAAmB,MAAM,UAAU,CAAC;AACxD,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAOjD,oBAAY,QAAQ,GAAG,IAAI,CAAC,iBAAiB,EAAE,MAAM,GAAG,IAAI,GAAG,QAAQ,CAAC,CAAC;AAEzE,oBAAY,UAAU,GAAG;IACvB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,EAAE,CAAC;CAChB,CAAC;
|
|
1
|
+
{"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../src/cache.ts"],"names":[],"mappings":"AACA,OAAkB,EAAE,WAAW,EAAE,QAAQ,IAAI,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAEnF,OAAO,EAAE,WAAW,EAAmB,MAAM,UAAU,CAAC;AACxD,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AA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imeV2 = exports.getFetchRmsRuntimeV1 = exports.Cache = exports.getResponseBody = void 0;
|
|
3
|
+
exports.connect = exports.createFetchRmsRuntimeV2 = exports.getFetchRmsRuntimeV1 = exports.Cache = exports.getResponseBody = exports.ApiError = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const https_1 = require("https");
|
|
6
6
|
const node_fetch_1 = tslib_1.__importDefault(require("node-fetch"));
|
|
7
7
|
const api_1 = require("@forge/api");
|
|
8
8
|
const tunnel_1 = require("./tunnel");
|
|
9
9
|
const __localStore = new Map();
|
|
10
|
+
class ApiError extends Error {
|
|
11
|
+
constructor(status, code, message, data) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.status = status;
|
|
14
|
+
this.code = code;
|
|
15
|
+
this.data = data;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.ApiError = ApiError;
|
|
10
19
|
async function getResponseBody(response) {
|
|
20
|
+
var _a, _b, _c;
|
|
11
21
|
const responseText = await response.text();
|
|
12
22
|
if (!response.ok) {
|
|
13
|
-
|
|
23
|
+
try {
|
|
24
|
+
const parsedError = JSON.parse(responseText);
|
|
25
|
+
throw new ApiError(response.status, (_a = parsedError === null || parsedError === void 0 ? void 0 : parsedError.error) === null || _a === void 0 ? void 0 : _a.code, `"title":"${(_b = parsedError === null || parsedError === void 0 ? void 0 : parsedError.error) === null || _b === void 0 ? void 0 : _b.title}"`, (_c = parsedError === null || parsedError === void 0 ? void 0 : parsedError.error) === null || _c === void 0 ? void 0 : _c.data);
|
|
26
|
+
}
|
|
27
|
+
catch (e) {
|
|
28
|
+
if (!(e instanceof ApiError)) {
|
|
29
|
+
throw new ApiError(response.status, 'UNKNOWN_ERROR', `${responseText}`);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
throw e;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
14
35
|
}
|
|
15
36
|
try {
|
|
16
37
|
return JSON.parse(responseText);
|