@nestia/fetcher
Version:
Fetcher library of Nestia SDK
98 lines • 5.57 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EncryptedFetcher = void 0;
const AesPkcs5_1 = require("./AesPkcs5");
const FetcherBase_1 = require("./internal/FetcherBase");
/**
* Utility class for `fetch` functions used in `@nestia/sdk` with encryption.
*
* `EncryptedFetcher` is a utility class designed for SDK functions generated by
* [`@nestia/sdk`](https://nestia.io/docs/sdk/sdk), interacting with the remote
* HTTP API encrypted by AES-PKCS algorithm. In other words, this is a
* collection of dedicated `fetch()` functions for `@nestia/sdk` with
* encryption.
*
* For reference, `EncryptedFetcher` class being used only when target
* controller method is encrypting body data by `@EncryptedRoute` or
* `@EncryptedBody` decorators. If those decorators are not used,
* {@link PlainFetcher} class would be used instead.
*
* @author Jeongho Nam - https://github.com/samchon
*/
var EncryptedFetcher;
(function (EncryptedFetcher) {
function fetch(connection, route, input, stringify) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d;
if ((((_a = route.request) === null || _a === void 0 ? void 0 : _a.encrypted) === true || ((_b = route.response) === null || _b === void 0 ? void 0 : _b.encrypted)) &&
connection.encryption === undefined)
throw new Error("Error on EncryptedFetcher.fetch(): the encryption password has not been configured.");
const closure = typeof connection.encryption === "function"
? (direction) => (headers, body) => connection.encryption({
headers,
body,
direction,
})
: () => () => connection.encryption;
return FetcherBase_1.FetcherBase.request({
className: "EncryptedFetcher",
encode: ((_c = route.request) === null || _c === void 0 ? void 0 : _c.encrypted) === true
? (input, headers) => {
const p = closure("encode")(headers, input);
return AesPkcs5_1.AesPkcs5.encrypt((stringify !== null && stringify !== void 0 ? stringify : JSON.stringify)(input), p.key, p.iv);
}
: (input) => input,
decode: ((_d = route.response) === null || _d === void 0 ? void 0 : _d.encrypted) === true
? (input, headers) => {
const p = closure("decode")(headers, input);
const s = AesPkcs5_1.AesPkcs5.decrypt(input, p.key, p.iv);
return s.length ? JSON.parse(s) : s;
}
: (input) => input,
})(connection, route, input, stringify);
});
}
EncryptedFetcher.fetch = fetch;
function propagate(connection, route, input, stringify) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d;
if ((((_a = route.request) === null || _a === void 0 ? void 0 : _a.encrypted) === true || ((_b = route.response) === null || _b === void 0 ? void 0 : _b.encrypted)) &&
connection.encryption === undefined)
throw new Error("Error on EncryptedFetcher.propagate(): the encryption password has not been configured.");
const closure = typeof connection.encryption === "function"
? (direction) => (headers, body) => connection.encryption({
headers,
body,
direction,
})
: () => () => connection.encryption;
return FetcherBase_1.FetcherBase.propagate({
className: "EncryptedFetcher",
encode: ((_c = route.request) === null || _c === void 0 ? void 0 : _c.encrypted) === true
? (input, headers) => {
const p = closure("encode")(headers, input);
return AesPkcs5_1.AesPkcs5.encrypt((stringify !== null && stringify !== void 0 ? stringify : JSON.stringify)(input), p.key, p.iv);
}
: (input) => input,
decode: ((_d = route.response) === null || _d === void 0 ? void 0 : _d.encrypted) === true
? (input, headers) => {
const p = closure("decode")(headers, input);
const s = AesPkcs5_1.AesPkcs5.decrypt(input, p.key, p.iv);
return s.length ? JSON.parse(s) : s;
}
: (input) => input,
})(connection, route, input, stringify);
});
}
EncryptedFetcher.propagate = propagate;
})(EncryptedFetcher || (exports.EncryptedFetcher = EncryptedFetcher = {}));
//# sourceMappingURL=EncryptedFetcher.js.map