Skip to content

Commit da3662b

Browse files
committed
refactor: move type to a centrol places with different file
Signed-off-by: seven <[email protected]>
1 parent a8b4023 commit da3662b

File tree

11 files changed

+161
-153
lines changed

11 files changed

+161
-153
lines changed

src/stack/iacSchema.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { RawServerlessIac } from '../types';
21
import Ajv, { ErrorObject } from 'ajv';
32
import { logger } from '../common';
3+
import { ServerlessIacRaw } from '../types';
44

55
const ajv = new Ajv({ allowUnionTypes: true, strict: false, allErrors: true });
66

@@ -180,7 +180,7 @@ class IacSchemaErrors extends Error {
180180
}
181181
}
182182

183-
export const validateYaml = (iacJson: RawServerlessIac) => {
183+
export const validateYaml = (iacJson: ServerlessIacRaw) => {
184184
const validate = ajv.compile(schema);
185185
const valid = validate(iacJson);
186186
if (!valid) {

src/stack/parse.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { parse } from 'yaml';
22
import { existsSync, readFileSync } from 'node:fs';
33
import {
4+
DatabaseDomain,
45
DatabaseEnum,
5-
Event,
6-
IacDatabase,
7-
IacFunction,
8-
RawIacDatabase,
9-
RawServerlessIac,
6+
DatabaseRaw,
7+
EventDomain,
8+
FunctionDomain,
109
ServerlessIac,
10+
ServerlessIacRaw,
1111
} from '../types';
1212
import { validateYaml } from './iacSchema';
1313
import { get, isEmpty } from 'lodash';
@@ -34,8 +34,8 @@ const validateExistence = (path: string) => {
3434
}
3535
};
3636
const transformDatabase = (databases?: {
37-
[key: string]: RawIacDatabase;
38-
}): Array<IacDatabase> | undefined => {
37+
[key: string]: DatabaseRaw;
38+
}): Array<DatabaseDomain> | undefined => {
3939
if (isEmpty(databases)) {
4040
return undefined;
4141
}
@@ -57,15 +57,15 @@ const transformDatabase = (databases?: {
5757
},
5858
}));
5959
};
60-
const transformYaml = (iacJson: RawServerlessIac): ServerlessIac => {
60+
const transformYaml = (iacJson: ServerlessIacRaw): ServerlessIac => {
6161
return {
6262
service: iacJson.service,
6363
version: iacJson.version,
6464
provider: iacJson.provider,
6565
vars: iacJson.vars,
6666
stages: iacJson.stages,
67-
functions: mapToArr(iacJson.functions) as unknown as Array<IacFunction>,
68-
events: mapToArr(iacJson.events) as unknown as Array<Event>,
67+
functions: mapToArr(iacJson.functions) as unknown as Array<FunctionDomain>,
68+
events: mapToArr(iacJson.events) as unknown as Array<EventDomain>,
6969
tags: [
7070
{ key: 'iac-provider', value: 'ServerlessInsight' },
7171
...mapToKvArr(iacJson.tags),
@@ -78,7 +78,7 @@ export const parseYaml = (yamlPath: string): ServerlessIac => {
7878
validateExistence(yamlPath);
7979

8080
const yamlContent = readFileSync(yamlPath, 'utf8');
81-
const iacJson = parse(yamlContent) as RawServerlessIac;
81+
const iacJson = parse(yamlContent) as ServerlessIacRaw;
8282

8383
validateYaml(iacJson);
8484

src/types.ts

Lines changed: 0 additions & 131 deletions
This file was deleted.

src/types/context.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export type ActionContext = {
2+
stage: string;
3+
stackName: string;
4+
region: string;
5+
accessKeyId: string;
6+
accessKeySecret: string;
7+
securityToken?: string;
8+
iacLocation: string;
9+
parameters?: Array<{ key: string; value: string }>;
10+
tags?: Array<{ key: string; value: string }>;
11+
};
12+
13+
export enum TemplateFormat {
14+
YAML = 'YAML',
15+
JSON = 'JSON',
16+
}

src/types/database.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
export enum DatabaseEngineMode {
2+
SEARCH = 'SEARCH',
3+
TIMESERIES = 'TIMESERIES',
4+
}
5+
export enum DatabaseEnum {
6+
ELASTICSEARCH_SERVERLESS = 'ELASTICSEARCH_SERVERLESS',
7+
}
8+
9+
export type DatabaseRaw = {
10+
name: string;
11+
type: DatabaseEnum;
12+
version: string;
13+
engine_mode: DatabaseEngineMode;
14+
security: {
15+
basic_auth: {
16+
password: string;
17+
};
18+
};
19+
network?: {
20+
public: boolean;
21+
};
22+
cu: number;
23+
storage_size: number;
24+
};
25+
26+
export type DatabaseDomain = {
27+
key: string;
28+
name: string;
29+
type: DatabaseEnum;
30+
version: string;
31+
engineMode: string;
32+
security: {
33+
basicAuth: {
34+
password: string;
35+
};
36+
};
37+
network?: {
38+
public: boolean;
39+
};
40+
cu: number;
41+
storageSize: number;
42+
};

src/types/event.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export enum EventTypes {
2+
API_GATEWAY = 'API_GATEWAY',
3+
}
4+
5+
export type EventRaw = {
6+
name: string;
7+
type: EventTypes;
8+
triggers: Array<{
9+
method: string;
10+
path: string;
11+
backend: string;
12+
}>;
13+
};
14+
15+
export type EventDomain = {
16+
key: string;
17+
} & EventRaw;

src/types/function.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export type FunctionRaw = {
2+
name: string;
3+
key: string;
4+
runtime: string;
5+
handler: string;
6+
code: string;
7+
memory: number;
8+
timeout: number;
9+
environment: {
10+
[key: string]: string;
11+
};
12+
};
13+
14+
export type FunctionDomain = FunctionRaw;

src/types/index.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { Stages, Vars } from './vars';
2+
import { Tags } from './tag';
3+
import { EventDomain, EventRaw } from './event';
4+
import { DatabaseDomain, DatabaseRaw } from './database';
5+
import { FunctionDomain, FunctionRaw } from './function';
6+
7+
export type ServerlessIacRaw = {
8+
version: string;
9+
provider: string;
10+
vars: Vars;
11+
stages: Stages;
12+
service: string;
13+
tags: Tags;
14+
functions: { [key: string]: FunctionRaw };
15+
events: { [key: string]: EventRaw };
16+
databases: { [key: string]: DatabaseRaw };
17+
};
18+
19+
export type ServerlessIac = {
20+
version: string;
21+
provider: string;
22+
service: string;
23+
vars?: Vars;
24+
stages?: Stages;
25+
tags?: Array<{ key: string; value: string }>;
26+
functions?: Array<FunctionDomain>;
27+
events?: Array<EventDomain>;
28+
databases?: Array<DatabaseDomain>;
29+
};
30+
31+
export * from './database';
32+
export * from './event';
33+
export * from './function';
34+
export * from './tag';
35+
export * from './vars';
36+
export * from './context';

src/types/tag.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export type Tags = {
2+
[key: string]: string;
3+
};

src/types/vars.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export type Vars = {
2+
[key: string]: unknown;
3+
};
4+
5+
export type Stage = {
6+
[key: string]: string;
7+
};
8+
9+
export type Stages = {
10+
[key: string]: Stage;
11+
};

0 commit comments

Comments
 (0)