diff --git a/API.md b/API.md index d81363c..641b31e 100644 --- a/API.md +++ b/API.md @@ -3,6 +3,73 @@ ## Structs +### CustomNaming + +#### Initializer + +```typescript +import { ResourceNaming } from '@gammarers/aws-resource-naming' + +const customNaming: ResourceNaming.CustomNaming = { ... } +``` + +#### Properties + +| **Name** | **Type** | **Description** | +| --- | --- | --- | +| names | {[ key: string ]: string} | *No description.* | +| type | @gammarers/aws-resource-naming.ResourceNaming.NamingType | *No description.* | + +--- + +##### `names`Required + +```typescript +public readonly names: {[ key: string ]: string}; +``` + +- *Type:* {[ key: string ]: string} + +--- + +##### `type`Required + +```typescript +public readonly type: NamingType; +``` + +- *Type:* @gammarers/aws-resource-naming.ResourceNaming.NamingType + +--- + +### DefaultNaming + +#### Initializer + +```typescript +import { ResourceNaming } from '@gammarers/aws-resource-naming' + +const defaultNaming: ResourceNaming.DefaultNaming = { ... } +``` + +#### Properties + +| **Name** | **Type** | **Description** | +| --- | --- | --- | +| type | @gammarers/aws-resource-naming.ResourceNaming.NamingType | *No description.* | + +--- + +##### `type`Required + +```typescript +public readonly type: NamingType; +``` + +- *Type:* @gammarers/aws-resource-naming.ResourceNaming.NamingType + +--- + ### NamingOptions #### Initializer @@ -17,17 +84,45 @@ const namingOptions: ResourceNaming.NamingOptions = { ... } | **Name** | **Type** | **Description** | | --- | --- | --- | -| naming | @gammarers/aws-resource-naming.ResourceNaming.NamingType \| {[ key: string ]: string} | *No description.* | +| naming | @gammarers/aws-resource-naming.ResourceNaming.NoNaming \| @gammarers/aws-resource-naming.ResourceNaming.DefaultNaming \| @gammarers/aws-resource-naming.ResourceNaming.CustomNaming | *No description.* | --- ##### `naming`Required ```typescript -public readonly naming: NamingType | {[ key: string ]: string}; +public readonly naming: NoNaming | DefaultNaming | CustomNaming; ``` -- *Type:* @gammarers/aws-resource-naming.ResourceNaming.NamingType | {[ key: string ]: string} +- *Type:* @gammarers/aws-resource-naming.ResourceNaming.NoNaming | @gammarers/aws-resource-naming.ResourceNaming.DefaultNaming | @gammarers/aws-resource-naming.ResourceNaming.CustomNaming + +--- + +### NoNaming + +#### Initializer + +```typescript +import { ResourceNaming } from '@gammarers/aws-resource-naming' + +const noNaming: ResourceNaming.NoNaming = { ... } +``` + +#### Properties + +| **Name** | **Type** | **Description** | +| --- | --- | --- | +| type | @gammarers/aws-resource-naming.ResourceNaming.NamingType | *No description.* | + +--- + +##### `type`Required + +```typescript +public readonly type: NamingType; +``` + +- *Type:* @gammarers/aws-resource-naming.ResourceNaming.NamingType --- @@ -41,17 +136,23 @@ public readonly naming: NamingType | {[ key: string ]: string}; | **Name** | **Description** | | --- | --- | +| NO | *No description.* | | DEFAULT | *No description.* | -| NONE | *No description.* | +| CUSTOM | *No description.* | --- +##### `NO` + +--- + + ##### `DEFAULT` --- -##### `NONE` +##### `CUSTOM` --- diff --git a/src/index.ts b/src/index.ts index f6f2d28..1376b54 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,8 +10,9 @@ import * as crypto from 'crypto'; export namespace ResourceNaming { export enum NamingType { + NO, DEFAULT, - NONE, + CUSTOM, } //export interface Naming {} @@ -19,23 +20,43 @@ export namespace ResourceNaming { // [key: string]: string; // } - // export interface CustomNaming { - // // [key: string]: string; jsii error - // readonly type: NamingType.CUSTOM; - // readonly names: { - // [key: string]: string; - // }; - // } - export interface NamingOptions { - readonly naming: ResourceNaming.NamingType | { + export interface NoNaming { + readonly type: NamingType.NO; + } + + export interface DefaultNaming { + readonly type: NamingType.DEFAULT; + } + + export interface CustomNaming { + // [key: string]: string; jsii error + readonly type: NamingType.CUSTOM; + readonly names: { [key: string]: string; }; } - export function isNamingType(value: NamingType | {[key: string]: string}): value is NamingType { - return Object.values(NamingType).includes(value as NamingType); + export interface NamingOptions { + readonly naming: NoNaming | DefaultNaming | CustomNaming; } + // // jsii error JSII1006 + // export interface NamingOptions { + // // jsii error JSII1003 + // readonly naming: ResourceNaming.NamingType | { + // // [key: string]: string; + // [K in T]: string; + // }; + // } + + // export interface Txx { + // readonly namingOption: NamingOptions<'a'|'b'>; + // } + + // export function isNamingType(value: NamingType | {[key: string]: string}): value is NamingType { + // return Object.values(NamingType).includes(value as NamingType); + // } + export function createRandomString(value: crypto.BinaryLike, length: number = 8) { return crypto.createHash('shake256', { outputLength: (length / 2) }) .update(value) @@ -47,19 +68,32 @@ export namespace ResourceNaming { // return obj[key]; // } - export function naming (resourceNaming: NamingOptions, defaultNaming: {[p: string]: string | undefined}) { + //export function naming(resourceNaming: NamingOptions, defaultNaming: {[p: string]: string | undefined}) { + export function naming(resourceNaming: NamingOptions, defaultNaming: {[p: string]: string | undefined}) { const names = Object.fromEntries( Object.entries(defaultNaming).map(([name, value]) => { return [name, (() => { - if (ResourceNaming.isNamingType(resourceNaming.naming)) { - if (resourceNaming.naming === ResourceNaming.NamingType.DEFAULT) { + switch (resourceNaming.naming.type) { + case ResourceNaming.NamingType.CUSTOM: + return resourceNaming.naming.names[name as keyof {[key: string]: string}]; + case ResourceNaming.NamingType.DEFAULT: return value; - } - if (resourceNaming.naming === ResourceNaming.NamingType.NONE) { + case ResourceNaming.NamingType.NO: return undefined; - } } - return resourceNaming.naming[name as keyof {[key: string]: string}]; + // if (ResourceNaming.isNamingType(resourceNaming.naming.type)) { + // if (resourceNaming.naming.type === ResourceNaming.NamingType.DEFAULT) { + // return value; + // } + // if (resourceNaming.naming.type === ResourceNaming.NamingType.NO) { + // return undefined; + // } + // return resourceNaming.naming.names[name as keyof {[key: string]: string}]; + // } + // return resourceNaming.naming[name as keyof {[K in T]: string}]; + // return resourceNaming.naming.names[name as keyof {[key: string]: string}]; + + // return undefined; })()]; }), ); diff --git a/test/naming.test.ts b/test/naming.test.ts index 0068ee9..4db8fbe 100644 --- a/test/naming.test.ts +++ b/test/naming.test.ts @@ -18,9 +18,13 @@ describe('ResouceNaming Testing', () => { roleName: `${random}-func-exc-role`, }; + // type Names = 'functionName' | 'roleName'; + it('Is Naming Default include Randmon String', () => { const options: ResourceNaming.NamingOptions = { - naming: ResourceNaming.NamingType.DEFAULT, + naming: { + type: ResourceNaming.NamingType.DEFAULT, + }, }; const naming = ResourceNaming.naming(options, defaultNaming); expect(naming).toEqual({ @@ -30,7 +34,9 @@ describe('ResouceNaming Testing', () => { it('Is Naming undefined', () => { const options: ResourceNaming.NamingOptions = { - naming: ResourceNaming.NamingType.NONE, + naming: { + type: ResourceNaming.NamingType.NO, + }, }; const naming = ResourceNaming.naming(options, defaultNaming); expect(naming).toEqual({ @@ -44,8 +50,11 @@ describe('ResouceNaming Testing', () => { it('Is Namings', () => { const options: ResourceNaming.NamingOptions = { naming: { - functionName: 'example-function', - roleName: 'example-role', + type: ResourceNaming.NamingType.CUSTOM, + names: { + functionName: 'example-function', + roleName: 'example-role', + }, }, }; const naming = ResourceNaming.naming(options, defaultNaming);