Skip to content

Commit febb213

Browse files
Fix: github reduce the code
1 parent 90ca225 commit febb213

31 files changed

+696
-283
lines changed

engine-edk/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@poozle/engine-edk",
3-
"version": "0.2.2",
3+
"version": "0.2.9",
44
"description": "Used to develop extensions for Poozle",
55
"license": "MIT",
66
"author": "Poozle <support@poozle.in>",
@@ -37,6 +37,7 @@
3737
"eslint-plugin-notice": "^0.9.10",
3838
"eslint-plugin-prettier": "^4.2.1",
3939
"eslint-plugin-unused-imports": "^2.0.0",
40+
"json-schema-to-typescript": "^13.0.2",
4041
"prettier": "^2.6.2",
4142
"rollup": "^3.2.3",
4243
"rollup-plugin-dts": "^5.2.0",

engine-edk/src/bases/base_model.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { AxiosHeaders } from 'axios';
44
import { Config, Params } from 'types/integration';
55

66
import { BaseModelInterface, Schema } from 'types/model';
7+
import { BasePath } from './base_path';
78

89
export class BaseModel implements BaseModelInterface {
910
name: string;
@@ -21,17 +22,24 @@ export class BaseModel implements BaseModelInterface {
2122
return pathExists ? true : false;
2223
}
2324

24-
paths(): any[] {
25+
paths(): BasePath<any>[] {
2526
return [];
2627
}
2728

2829
async run(path: string, method: string, headers: AxiosHeaders, params: Params, config: Config) {
2930
try {
3031
const paths = this.paths();
3132

32-
const pathToRun = paths.find((p) => p.isPath(path, method));
33+
const pathToRun: BasePath<any> | undefined = paths.find((p) => p.isPath(path, method));
3334

34-
return await pathToRun.run(method, headers, params, config);
35+
if (pathToRun) {
36+
return await pathToRun.baseRun(method, headers, params, config);
37+
}
38+
39+
return {
40+
status: 'error',
41+
error: 'Path not found',
42+
};
3543
} catch (err) {
3644
return {
3745
status: 'error',

engine-edk/src/bases/base_path.ts

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import { AxiosHeaders } from 'axios';
44

55
import { Config, Params } from 'types/integration';
6+
import { Meta, PathResponse, Response } from 'types/path';
7+
import { convertToModelKeys } from 'utils';
68

7-
export class BasePath {
9+
export class BasePath<T> {
810
pathRegex: RegExp;
911
method: string | string[];
1012
schema: Record<string, any>;
@@ -28,10 +30,55 @@ export class BasePath {
2830
return this.pathRegex.test(path) && this.isMatchingMethod(method);
2931
}
3032

33+
convertToModel(data: any, raw: boolean) {
34+
const raw_data = data['raw_data'];
35+
36+
return convertToModelKeys(data, this.schema, raw_data, raw);
37+
}
38+
39+
async baseRun(
40+
method: string,
41+
headers: AxiosHeaders,
42+
params: Params,
43+
config: Config,
44+
): Promise<Response<T[] | T>> {
45+
const responseFromRun = await this.run(method, headers, params, config);
46+
47+
if (Array.isArray(responseFromRun)) {
48+
const data = responseFromRun.map((responseItem: any) =>
49+
this.convertToModel(responseItem, params.queryParams?.raw ? true : false),
50+
);
51+
52+
const meta = await this.getMetaParams(data, params);
53+
54+
return {
55+
data,
56+
meta,
57+
};
58+
} else {
59+
return { data: this.convertToModel(responseFromRun, params.queryParams?.raw ? true : false) };
60+
}
61+
}
62+
63+
// Written by the integration
64+
async getMetaParams(_data: T[], _params: Params): Promise<Meta> {
65+
return {
66+
limit: 0,
67+
cursors: {
68+
before: '0',
69+
current: '0',
70+
next: '0',
71+
},
72+
};
73+
}
74+
75+
// Written by the integration
3176
async run(
3277
_method: string,
3378
_headers: AxiosHeaders,
3479
_params: Params,
3580
_config: Config,
36-
): Promise<any> {}
81+
): Promise<PathResponse<any>> {
82+
return [];
83+
}
3784
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/** Copyright (c) 2023, Poozle, all rights reserved. **/
22

3-
export { TicketSchema } from './ticket';
4-
export { CollectionSchema } from './collection';
5-
export { CommentSchema } from './comment';
6-
export { TagSchema } from './tag';
7-
export { TeamSchema } from './team';
8-
export { UserSchema } from './user';
3+
export * from './ticket';
4+
export * from './collection';
5+
export * from './comment';
6+
export * from './tag';
7+
export * from './team';
8+
export * from './user';

engine-edk/src/common_models/ticketing/ticket.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const TicketSchema = {
1919
type: 'string',
2020
default: '',
2121
},
22-
subject: {
22+
name: {
2323
type: 'string',
2424
default: '',
2525
},
@@ -101,3 +101,47 @@ export const TicketSchema = {
101101
},
102102
},
103103
};
104+
105+
export interface Assignee {
106+
id: string;
107+
username: string;
108+
}
109+
110+
export interface Tag {
111+
id: string;
112+
name: string;
113+
}
114+
115+
export interface Ticket {
116+
id: number;
117+
parent_id: string;
118+
collection_id: string;
119+
type: string;
120+
name: string;
121+
description: string;
122+
status: string;
123+
priority: string;
124+
ticket_url: string;
125+
assignees: Assignee[];
126+
updated_at: string;
127+
created_at: string;
128+
created_by: string;
129+
due_date: string;
130+
completed_at: string;
131+
tags: Tag[];
132+
}
133+
134+
export interface CreateTicketBody {
135+
name: string;
136+
description: string;
137+
assignees: Assignee[];
138+
tags: Tag[];
139+
}
140+
141+
export interface UpdateTicketBody {
142+
name: string;
143+
description: string;
144+
assignees: Assignee[];
145+
tags: Tag[];
146+
status: string;
147+
}

engine-edk/src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22

33
export * from './integration';
44
export * from './model';
5+
export * from './path';

engine-edk/src/types/path.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export interface Meta {
2+
limit: number;
3+
cursors: {
4+
before: string;
5+
current: string;
6+
next: string;
7+
};
8+
}
9+
10+
export interface Response<T> {
11+
data: Partial<T>;
12+
meta?: Meta;
13+
error?: string;
14+
}
15+
16+
export interface ResponseArray<T> {
17+
data: Partial<T>[];
18+
meta?: Meta;
19+
error?: string;
20+
}
21+
22+
export type PathResponse<T> = Partial<T> | { raw_data: Record<any, any> };

engine-edk/src/utils/proxy_path.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class GenericProxyModel extends BaseModel {
1818
}
1919
}
2020

21-
export class ProxyPath extends BasePath {
21+
export class ProxyPath extends BasePath<any> {
2222
async run(method: string, headers: AxiosHeaders, params: Params): Promise<any> {
2323
const axiosObject: any = {
2424
url: params.url,

0 commit comments

Comments
 (0)