@nestia/fetcher
Version:
Fetcher library of Nestia SDK
84 lines (81 loc) • 3.83 kB
JavaScript
import { AesPkcs5 } from './AesPkcs5.mjs';
import { FetcherBase } from './internal/FetcherBase.mjs';
/**
* 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) {
async function fetch(connection, route, input, stringify) {
if ((route.request?.encrypted === true || route.response?.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.request({
className: "EncryptedFetcher",
encode: route.request?.encrypted === true
? (input, headers) => {
const p = closure("encode")(headers, input);
return AesPkcs5.encrypt((stringify ?? JSON.stringify)(input), p.key, p.iv);
}
: (input) => input,
decode: route.response?.encrypted === true
? (input, headers) => {
const p = closure("decode")(headers, input);
const s = AesPkcs5.decrypt(input, p.key, p.iv);
return s.length ? JSON.parse(s) : s;
}
: (input) => input,
})(connection, route, input, stringify);
}
EncryptedFetcher.fetch = fetch;
async function propagate(connection, route, input, stringify) {
if ((route.request?.encrypted === true || route.response?.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.propagate({
className: "EncryptedFetcher",
encode: route.request?.encrypted === true
? (input, headers) => {
const p = closure("encode")(headers, input);
return AesPkcs5.encrypt((stringify ?? JSON.stringify)(input), p.key, p.iv);
}
: (input) => input,
decode: route.response?.encrypted === true
? (input, headers) => {
const p = closure("decode")(headers, input);
const s = AesPkcs5.decrypt(input, p.key, p.iv);
return s.length ? JSON.parse(s) : s;
}
: (input) => input,
})(connection, route, input, stringify);
}
EncryptedFetcher.propagate = propagate;
})(EncryptedFetcher || (EncryptedFetcher = {}));
export { EncryptedFetcher };
//# sourceMappingURL=EncryptedFetcher.mjs.map