From cfa412b6c18d39cf907e137296c555eaee33fd1f Mon Sep 17 00:00:00 2001 From: Zyb Jared Valdez Date: Thu, 23 May 2024 10:42:51 +0800 Subject: [PATCH] dprint fmt --- types/mastercard-client-encryption/index.d.ts | 316 +++++++++--------- .../mastercard-client-encryption-tests.ts | 210 ++++++------ 2 files changed, 261 insertions(+), 265 deletions(-) diff --git a/types/mastercard-client-encryption/index.d.ts b/types/mastercard-client-encryption/index.d.ts index 0f54b631fca04b..c944f6f112d282 100644 --- a/types/mastercard-client-encryption/index.d.ts +++ b/types/mastercard-client-encryption/index.d.ts @@ -1,53 +1,52 @@ export interface DecryptOptions { body: unknown; request: { - url: string + url: string; }; } export type PublicKeyFingerprintType = - | 'certificate' - | 'publicKey' + | "certificate" + | "publicKey"; export type DataEncodingType = - | 'base64' - | 'hex' + | "base64" + | "hex"; export interface RootMapping { - element: string, - obj: string + element: string; + obj: string; } export type EncryptionParameter = | RootMapping[] - | [] + | []; export interface RootMappingNode { - path: string, - toEncrypt: EncryptionParameter, - toDecrypt: EncryptionParameter, + path: string; + toEncrypt: EncryptionParameter; + toDecrypt: EncryptionParameter; } export type KeyStoreFileExtension = - | '.p12' - | '.pem' - | '.der' + | ".p12" + | ".pem" + | ".der"; -export type KeyStoreFile = `${string}${KeyStoreFileExtension}` +export type KeyStoreFile = `${string}${KeyStoreFileExtension}`; export interface JweEncryptionConfiguration { - mode: string, - encryptionCertificate: unknown, - encryptedValueFieldName: unknown, - paths: RootMappingNode[], - publicKeyFingerprintType?: PublicKeyFingerprintType - publicKeyFingerprint?: PublicKeyFingerprintType extends 'certificate' - ? DataEncodingType - : unknown, - privateKey?: unknown, - keyStore?: KeyStoreFile, - keyStoreAlias?: unknown, - keyStorePassword?: unknown, + mode: string; + encryptionCertificate: unknown; + encryptedValueFieldName: unknown; + paths: RootMappingNode[]; + publicKeyFingerprintType?: PublicKeyFingerprintType; + publicKeyFingerprint?: PublicKeyFingerprintType extends "certificate" ? DataEncodingType + : unknown; + privateKey?: unknown; + keyStore?: KeyStoreFile; + keyStoreAlias?: unknown; + keyStorePassword?: unknown; } /** @@ -70,11 +69,11 @@ export class JweEncryption { encrypt: ( url: string, headers: Record, - body: T + body: T, ) => { - headers: Record, - body: T - } + headers: Record; + body: T; + }; /** * Decrypt part of the HTTP response using the given config @@ -82,7 +81,7 @@ export class JweEncryption { * @param response HTTP response to decrypt * @returns {*} */ - decrypt: (response: T) => T['body'] + decrypt: (response: T) => T["body"]; } export namespace EncryptionUtils { @@ -90,7 +89,7 @@ export namespace EncryptionUtils { * Utils module * @version 1.0.0 */ - function isSet(value: unknown): boolean + function isSet(value: unknown): boolean; /** * Converts a 'binary' encoded string of bytes to (hex or base64) encoded string. @@ -101,8 +100,8 @@ export namespace EncryptionUtils { */ function bytesToString( bytes: string, - dataEncoding: DataEncodingType - ): string + dataEncoding: DataEncodingType, + ): string; /** * Converts a (hex or base64) string into a 'binary' encoded string of bytes. @@ -113,13 +112,13 @@ export namespace EncryptionUtils { */ function stringToBytes( value: string, - dataEncoding: DataEncodingType - ): string + dataEncoding: DataEncodingType, + ): string; function toByteArray( value: string, - fromFormat: import('node:buffer').TranscodeEncoding - ): Buffer + fromFormat: import("node:buffer").TranscodeEncoding, + ): Buffer; /** * Convert a json object or json string to string @@ -128,8 +127,8 @@ export namespace EncryptionUtils { * @returns {string} */ function jsonToString( - data: unknown - ): string + data: unknown, + ): string; /** * Convert Json string to Json object if it is a valid Json @@ -139,123 +138,121 @@ export namespace EncryptionUtils { * @returns {Object|string} */ function stringToJson( - data: string - ): unknown + data: string, + ): unknown; function mutateObjectProperty( path: string, value: unknown, obj: unknown, srcPath: NonNullable, - properties: string[] - ): unknown + properties: string[], + ): unknown; interface PrivateKeyConfig { - privateKey: JweEncryptionConfiguration['privateKey'], - keyStore?: never, - keyStoreAlias?: never, - keyStorePassword?: never + privateKey: JweEncryptionConfiguration["privateKey"]; + keyStore?: never; + keyStoreAlias?: never; + keyStorePassword?: never; } interface KeystoreConfig { - privateKey?: never, - keyStore: JweEncryptionConfiguration['keyStore'], - keyStoreAlias: JweEncryptionConfiguration['keyStoreAlias'], - keyStorePassword: JweEncryptionConfiguration['keyStorePassword'] + privateKey?: never; + keyStore: JweEncryptionConfiguration["keyStore"]; + keyStoreAlias: JweEncryptionConfiguration["keyStoreAlias"]; + keyStorePassword: JweEncryptionConfiguration["keyStorePassword"]; } - type getPrivateKeyConfig = PrivateKeyConfig | KeystoreConfig + type getPrivateKeyConfig = PrivateKeyConfig | KeystoreConfig; function getPrivateKey( - config: getPrivateKeyConfig + config: getPrivateKeyConfig, ): - | import('node-forge').pki.rsa.PrivateKey - | import('node-forge').pki.PrivateKey - | null - | undefined + | import("node-forge").pki.rsa.PrivateKey + | import("node-forge").pki.PrivateKey + | null + | undefined; - function readPublicCertificate ( - publicCertificatePath: string + function readPublicCertificate( + publicCertificatePath: string, ): - | import('node-forge').pki.Certificate - | undefined + | import("node-forge").pki.Certificate + | undefined; - function computePublicFingerprint < - T extends PublicKeyFingerprintType + function computePublicFingerprint< + T extends PublicKeyFingerprintType, >( config: { - publicKeyFingerprintType: T + publicKeyFingerprintType: T; }, - encryptionCertificate: T extends 'certificate' - ? import('node-forge').pki.Certificate - : Pick, - encoding: DataEncodingType - ): string + encryptionCertificate: T extends "certificate" ? import("node-forge").pki.Certificate + : Pick, + encoding: DataEncodingType, + ): string; - function nodeVersionSupportsJWE (): boolean + function nodeVersionSupportsJWE(): boolean; - function checkConfigFieldsArePopulated ( + function checkConfigFieldsArePopulated( config: { - paths: unknown + paths: unknown; }, propertiesBasic: string[], propertiesField: string[], propertiesHeader: string[], - ): boolean | undefined + ): boolean | undefined; function validateRootMapping( - config: Pick - ): void + config: Pick, + ): void; function hasConfig( config: unknown, - endpoint: string - ): string[] | null + endpoint: string, + ): string[] | null; function elemFromPath( path: string, - obj: unknown + obj: unknown, ): - | { node: unknown, parent: unknown } - | null + | { node: unknown; parent: unknown } + | null; function isJsonRoot( - elem: unknown - ): boolean + elem: unknown, + ): boolean; function computeBody( configParam: EncryptionParameter, body: unknown, bodyMap: unknown, - ): boolean + ): boolean; function addEncryptedDataToBody( encryptedData: unknown, path: RootMapping, encryptedValueFieldName: string, - body: unknown - ): unknown + body: unknown, + ): unknown; function readPublicCertificateContent( - config: string - ): import('node-forge').pki.Certificate + config: string, + ): import("node-forge").pki.Certificate; function getPrivateKeyFromContent( - config: T - ): T extends { privateKey: string } - ? import('node-forge').pki.PrivateKey - : null + config: T, + ): T extends { privateKey: string } ? import("node-forge").pki.PrivateKey + : null; } export type FieldLevelEncryptionConfiguration = JweEncryptionConfiguration & { - ivFieldName: string, - ivHeaderName?: string, - encryptedKeyFieldName: string, - encryptedKeyHeaderName?: string, - publicKeyFingerprintFieldName: string, - publicKeyFingerprintHeaderName?: string, - oaepHashingAlgorithmFieldName: string, -} + ivFieldName: string; + ivHeaderName?: string; + encryptedKeyFieldName: string; + encryptedKeyHeaderName?: string; + publicKeyFingerprintFieldName: string; + publicKeyFingerprintHeaderName?: string; + oaepHashingAlgorithmFieldName: string; +}; /** * Performs field level encryption on HTTP payloads. @@ -277,11 +274,11 @@ export class FieldLevelEncryption { encrypt( endpoint: string, header: Record, - body: T + body: T, ): { - header: Record, - body: T - } + header: Record; + body: T; + }; /** * Decrypt part of the HTTP response using the given config @@ -289,15 +286,15 @@ export class FieldLevelEncryption { * @param response HTTP response to decrypt * @returns {*} */ - decrypt: (response: T) => T['body'] + decrypt: (response: T) => T["body"]; } /** -* Manages low level client-server communications, parameter marshalling, etc. There should not be any need for an -* application to use this class directly - the *Api and model classes provide the public API for the service. The -* contents of this file should be regarded as internal but are documented for completeness. -* @alias module:ApiClient -*/ + * Manages low level client-server communications, parameter marshalling, etc. There should not be any need for an + * application to use this class directly - the *Api and model classes provide the public API for the service. The + * contents of this file should be regarded as internal but are documented for completeness. + * @alias module:ApiClient + */ export class ApiClient { /** * The base URL against which to resolve every API call's (relative) path. @@ -363,11 +360,10 @@ export class ApiClient { * @returns {String} The string representation of param. */ paramToString( - param: unknown - ): string + param: unknown, + ): string; /** - * * Returns a boolean indicating if the parameter could be JSON.stringified * @param param The actual parameter * @returns Flag indicating if param can be JSON.stringified @@ -385,8 +381,8 @@ export class ApiClient { buildUrl( path: string, pathParams: Record, - apiBasePath: NonNullable - ): string + apiBasePath: NonNullable, + ): string; /** * Checks whether the given content type represents JSON.
@@ -401,7 +397,7 @@ export class ApiClient { * @returns true if contentType represents JSON, otherwise false. */ isJsonMime( - contentType: string + contentType: string, ): boolean; /** @@ -410,8 +406,8 @@ export class ApiClient { * @returns The chosen content type, preferring JSON. */ jsonPreferredMime( - contentTypes: Record - ): string + contentTypes: Record, + ): string; /** * Checks whether the given parameter value represents file-like content. @@ -419,8 +415,8 @@ export class ApiClient { * @returns true if param represents a file. */ isFileParam( - param: import('fs').ReadStream | Buffer | Blob | File - ): boolean + param: import("fs").ReadStream | Buffer | Blob | File, + ): boolean; /** * Normalizes parameter values: @@ -433,8 +429,8 @@ export class ApiClient { * @returns normalized parameters. */ normalizeParams( - params: Record - ): Record + params: Record, + ): Record; /** * Builds a string representation of an array-type actual parameter, according to the given collection format. @@ -445,8 +441,8 @@ export class ApiClient { */ buildCollectionParam( param: unknown[], - collectionFormat: ApiClient.CollectionFormatEnum - ): string | [] | null | undefined + collectionFormat: ApiClient.CollectionFormatEnum, + ): string | [] | null | undefined; /** * Applies authentication headers to the request. @@ -455,8 +451,8 @@ export class ApiClient { */ applyAuthToRequest( request: unknown, // TODO: get type of request from superagent - authNames: string[] - ): void + authNames: string[], + ): void; /** * Deserializes an HTTP response body into a value of the specified type. @@ -469,8 +465,8 @@ export class ApiClient { */ deserialize( response: unknown, // TODO: get superagent response object shape - returnType: string | string[] | Record | ApiClient.Model - ): unknown + returnType: string | string[] | Record | ApiClient.Model, + ): unknown; /** * Converts a value to the specified type. @@ -485,7 +481,7 @@ export class ApiClient { */ static convertToType( data: unknown, - type: string | string[] | Record | ApiClient.Model + type: string | string[] | Record | ApiClient.Model, ): unknown; /** @@ -516,11 +512,11 @@ export class ApiClient { contentTypes: string[], accepts: string[], returnType: string | [] | ApiClient.Model | unknown, - apiBasePath: string + apiBasePath: string, ): Promise< - (TResponse & { data: unknown }) + | (TResponse & { data: unknown }) | ApiClient.ErrorResponse - > + >; /** * Parses an ISO-8601 string representation or epoch representation of a date value. @@ -529,12 +525,12 @@ export class ApiClient { */ static parseDate(str: string): Date; - hostSettings(): Array<{ url: string, description: string}>; + hostSettings(): Array<{ url: string; description: string }>; getBasePathFromSettings( index: number, - variables: Record - ): string | undefined + variables: Record, + ): string | undefined; /** * Constructs a new map or array model from REST data. @@ -545,8 +541,8 @@ export class ApiClient { static constructFromObject( data: unknown[] | unknown, obj: unknown[] | unknown, - type: string - ): void + type: string, + ): void; /** * The default API client implementation. @@ -555,51 +551,51 @@ export class ApiClient { } export namespace ApiClient { - type CollectionFormatEnum = 'csv' | 'ssv' | 'tsv' | 'pipes' | 'multi' | 'passthrough' + type CollectionFormatEnum = "csv" | "ssv" | "tsv" | "pipes" | "multi" | "passthrough"; interface Response { - status: unknown, - statusText: string, - body: unknown, - response: T + status: unknown; + statusText: string; + body: unknown; + response: T; } type ErrorResponse = Response & { - error: Error - } + error: Error; + }; interface Model { constructFromObject( data: unknown, - obj: unknown + obj: unknown, ): unknown; } } export type JweCryptoConfiguration = - { encryptionCertificate: string } + & { encryptionCertificate: string } & EncryptionUtils.getPrivateKeyConfig & ( - { - publicKeyFingerprint: unknown, - publicKeyFingerprintType?: never, - dataEncoding?: never, + | { + publicKeyFingerprint: unknown; + publicKeyFingerprintType?: never; + dataEncoding?: never; } | { - publicKeyFingerprint?: never, - publicKeyFingerprintType: PublicKeyFingerprintType, - dataEncoding: DataEncodingType, + publicKeyFingerprint?: never; + publicKeyFingerprintType: PublicKeyFingerprintType; + dataEncoding: DataEncodingType; } ) - & { encryptedValueFieldName: string } + & { encryptedValueFieldName: string }; export class JweCrypto { - encryptionCertificate: string; + encryptionCertificate: string; - privateKey: string | null; + privateKey: string | null; - publicKeyFingerprint: unknown | string | null; + publicKeyFingerprint: unknown | string | null; - encryptedValueFieldName: string; + encryptedValueFieldName: string; - constructor(options: JweCryptoConfiguration) + constructor(options: JweCryptoConfiguration); } diff --git a/types/mastercard-client-encryption/mastercard-client-encryption-tests.ts b/types/mastercard-client-encryption/mastercard-client-encryption-tests.ts index abd375de7d66fb..b69719819adf95 100644 --- a/types/mastercard-client-encryption/mastercard-client-encryption-tests.ts +++ b/types/mastercard-client-encryption/mastercard-client-encryption-tests.ts @@ -1,174 +1,174 @@ -import { JweEncryption, EncryptionUtils, FieldLevelEncryption, JweCrypto } from 'mastercard-client-encryption' +import { EncryptionUtils, FieldLevelEncryption, JweCrypto, JweEncryption } from "mastercard-client-encryption"; const jweEncryptionConfig = { - mode: 'JWE', + mode: "JWE", paths: [ { - path: '', + path: "", toEncrypt: [], - toDecrypt: [] - } + toDecrypt: [], + }, ], - encryptedValueFieldName: 'encryptedData', - encryptionCertificate: '', -} + encryptedValueFieldName: "encryptedData", + encryptionCertificate: "", +}; const jwe = new JweEncryption({ - mode: 'JWE', + mode: "JWE", paths: [ { - path: '', + path: "", toEncrypt: [], - toDecrypt: [] - } + toDecrypt: [], + }, ], - encryptedValueFieldName: 'encryptedData', - encryptionCertificate: '', -}) + encryptedValueFieldName: "encryptedData", + encryptionCertificate: "", +}); -jwe.encrypt('/', {}, {}); +jwe.encrypt("/", {}, {}); jwe.decrypt({ - request: { url: '' }, - body: {} + request: { url: "" }, + body: {}, }); -EncryptionUtils.bytesToString('', 'base64') -EncryptionUtils.bytesToString('', 'hex') +EncryptionUtils.bytesToString("", "base64"); +EncryptionUtils.bytesToString("", "hex"); -EncryptionUtils.stringToBytes('', 'base64') -EncryptionUtils.stringToBytes('', 'hex') +EncryptionUtils.stringToBytes("", "base64"); +EncryptionUtils.stringToBytes("", "hex"); -EncryptionUtils.stringToJson('{"name":""}') -EncryptionUtils.jsonToString({ name: '' }) +EncryptionUtils.stringToJson("{\"name\":\"\"}"); +EncryptionUtils.jsonToString({ name: "" }); -EncryptionUtils.toByteArray('', 'utf-8') +EncryptionUtils.toByteArray("", "utf-8"); -EncryptionUtils.isSet(null) -EncryptionUtils.isSet(undefined) -EncryptionUtils.isSet('') -EncryptionUtils.isSet([]) -EncryptionUtils.isSet(true) -EncryptionUtils.isSet(false) -EncryptionUtils.isSet(1) -EncryptionUtils.isSet(0) +EncryptionUtils.isSet(null); +EncryptionUtils.isSet(undefined); +EncryptionUtils.isSet(""); +EncryptionUtils.isSet([]); +EncryptionUtils.isSet(true); +EncryptionUtils.isSet(false); +EncryptionUtils.isSet(1); +EncryptionUtils.isSet(0); EncryptionUtils.getPrivateKey({ - privateKey: '' -}) + privateKey: "", +}); EncryptionUtils.getPrivateKey({ - keyStore: '.pem', - keyStoreAlias: '', - keyStorePassword: '', -}) + keyStore: ".pem", + keyStoreAlias: "", + keyStorePassword: "", +}); // @ts-expect-error -EncryptionUtils.getPrivateKeyFromContent({ keystore: '' }) +EncryptionUtils.getPrivateKeyFromContent({ keystore: "" }); -EncryptionUtils.getPrivateKeyFromContent({ privateKey: '' }) +EncryptionUtils.getPrivateKeyFromContent({ privateKey: "" }); -EncryptionUtils.readPublicCertificate('path/to/certificate') +EncryptionUtils.readPublicCertificate("path/to/certificate"); EncryptionUtils.computePublicFingerprint( - { publicKeyFingerprintType: 'certificate', }, + { publicKeyFingerprintType: "certificate" }, // @ts-expect-error - { publicKey: '' }, - 'base64' -) + { publicKey: "" }, + "base64", +); EncryptionUtils.computePublicFingerprint( - { publicKeyFingerprintType: 'publicKey' }, + { publicKeyFingerprintType: "publicKey" }, // @ts-ignore no public key initialization to test whole public key shape - { publicKey: '' }, - 'base64' -) + { publicKey: "" }, + "base64", +); -EncryptionUtils.nodeVersionSupportsJWE() +EncryptionUtils.nodeVersionSupportsJWE(); EncryptionUtils.checkConfigFieldsArePopulated( { paths: [] }, - ['path'], - ['field'], - ['header'] -) + ["path"], + ["field"], + ["header"], +); EncryptionUtils.validateRootMapping({ paths: [ { - path: '/', + path: "/", toEncrypt: [], - toDecrypt: [] - } - ] -}) + toDecrypt: [], + }, + ], +}); EncryptionUtils.hasConfig({ - paths: [] -}, '/') + paths: [], +}, "/"); -EncryptionUtils.elemFromPath('path.to.object', { +EncryptionUtils.elemFromPath("path.to.object", { to: { - object: '' - } -}) + object: "", + }, +}); -EncryptionUtils.isJsonRoot({}) +EncryptionUtils.isJsonRoot({}); EncryptionUtils.computeBody( [ - { element: '', obj: '' } + { element: "", obj: "" }, ], {}, - [] -) + [], +); EncryptionUtils.addEncryptedDataToBody( {}, - { element: '', obj: '', }, - '', - {} -) + { element: "", obj: "" }, + "", + {}, +); const fle = new FieldLevelEncryption({ ...jweEncryptionConfig, - ivFieldName: 'iv', - encryptedKeyFieldName: 'encryptedKey', - encryptedValueFieldName: 'encryptedData', - oaepHashingAlgorithmFieldName: 'SHA-256', - publicKeyFingerprintFieldName: '', -}) - -fle.encrypt('/', {}, {}) + ivFieldName: "iv", + encryptedKeyFieldName: "encryptedKey", + encryptedValueFieldName: "encryptedData", + oaepHashingAlgorithmFieldName: "SHA-256", + publicKeyFingerprintFieldName: "", +}); + +fle.encrypt("/", {}, {}); fle.decrypt({ - request: { url: '' }, - body: {} -}) + request: { url: "" }, + body: {}, +}); new JweCrypto({ - encryptionCertificate: '', - encryptedValueFieldName: '', + encryptionCertificate: "", + encryptedValueFieldName: "", - publicKeyFingerprint: '', + publicKeyFingerprint: "", - privateKey: '', -}) + privateKey: "", +}); new JweCrypto({ - encryptionCertificate: '', - encryptedValueFieldName: '', + encryptionCertificate: "", + encryptedValueFieldName: "", - publicKeyFingerprint: '', + publicKeyFingerprint: "", - keyStore: '.pem', - keyStoreAlias: '', - keyStorePassword: '' -}) + keyStore: ".pem", + keyStoreAlias: "", + keyStorePassword: "", +}); new JweCrypto({ - encryptionCertificate: '', - encryptedValueFieldName: '', + encryptionCertificate: "", + encryptedValueFieldName: "", - publicKeyFingerprintType: 'certificate', - dataEncoding: 'base64', + publicKeyFingerprintType: "certificate", + dataEncoding: "base64", - keyStore: '.der', - keyStoreAlias: '', - keyStorePassword: '' -}) + keyStore: ".der", + keyStoreAlias: "", + keyStorePassword: "", +});