@aneuhold/core-ts-api-lib 2.0.4 → 2.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/services/DOFunctionService/DOFunction.d.ts +11 -11
- package/lib/services/DOFunctionService/DOFunction.d.ts.map +1 -1
- package/lib/services/DOFunctionService/DOFunction.spec.ts +66 -0
- package/lib/services/DOFunctionService/DOFunction.ts +11 -11
- package/lib/services/DOFunctionService/DOFunctionService.spec.ts +61 -0
- package/package.json +1 -1
|
@@ -19,25 +19,25 @@ export interface DOFunctionRawInput {
|
|
|
19
19
|
*/
|
|
20
20
|
body: string;
|
|
21
21
|
headers: {
|
|
22
|
-
accept
|
|
23
|
-
'accept-encoding'
|
|
22
|
+
accept?: string;
|
|
23
|
+
'accept-encoding'?: string;
|
|
24
24
|
/**
|
|
25
25
|
* This is important.
|
|
26
26
|
*/
|
|
27
|
-
'content-type'
|
|
28
|
-
host
|
|
29
|
-
'user-agent'
|
|
30
|
-
'x-forwarded-for'
|
|
31
|
-
'x-forwarded-proto'
|
|
32
|
-
'x-request-id'
|
|
27
|
+
'content-type'?: string;
|
|
28
|
+
host?: string;
|
|
29
|
+
'user-agent'?: string;
|
|
30
|
+
'x-forwarded-for'?: string;
|
|
31
|
+
'x-forwarded-proto'?: string;
|
|
32
|
+
'x-request-id'?: string;
|
|
33
33
|
};
|
|
34
34
|
/**
|
|
35
35
|
* This needs to be used to determine if the body is base64 encoded.
|
|
36
36
|
*/
|
|
37
37
|
isBase64Encoded: boolean;
|
|
38
|
-
method
|
|
39
|
-
path
|
|
40
|
-
queryString
|
|
38
|
+
method?: string;
|
|
39
|
+
path?: string;
|
|
40
|
+
queryString?: string;
|
|
41
41
|
};
|
|
42
42
|
}
|
|
43
43
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DOFunction.d.ts","sourceRoot":"./src/","sources":["services/DOFunctionService/DOFunction.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG,MAAM,CAAC;AAErC;;;;GAIG;AACH,MAAM,MAAM,gBAAgB,GAAG,MAAM,CAAC;AAEtC;;;GAGG;AACH,MAAM,WAAW,kBAAkB;IACjC,IAAI,EAAE;QACJ;;WAEG;QACH,IAAI,EAAE,MAAM,CAAC;QACb,OAAO,EAAE;YACP,MAAM,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"DOFunction.d.ts","sourceRoot":"./src/","sources":["services/DOFunctionService/DOFunction.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG,MAAM,CAAC;AAErC;;;;GAIG;AACH,MAAM,MAAM,gBAAgB,GAAG,MAAM,CAAC;AAEtC;;;GAGG;AACH,MAAM,WAAW,kBAAkB;IACjC,IAAI,EAAE;QACJ;;WAEG;QACH,IAAI,EAAE,MAAM,CAAC;QACb,OAAO,EAAE;YACP,MAAM,CAAC,EAAE,MAAM,CAAC;YAChB,iBAAiB,CAAC,EAAE,MAAM,CAAC;YAC3B;;eAEG;YACH,cAAc,CAAC,EAAE,MAAM,CAAC;YACxB,IAAI,CAAC,EAAE,MAAM,CAAC;YACd,YAAY,CAAC,EAAE,MAAM,CAAC;YACtB,iBAAiB,CAAC,EAAE,MAAM,CAAC;YAC3B,mBAAmB,CAAC,EAAE,MAAM,CAAC;YAC7B,cAAc,CAAC,EAAE,MAAM,CAAC;SACzB,CAAC;QACF;;WAEG;QACH,eAAe,EAAE,OAAO,CAAC;QACzB,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,IAAI,CAAC,EAAE,MAAM,CAAC;QACd,WAAW,CAAC,EAAE,MAAM,CAAC;KACtB,CAAC;CACH;AAED;;;;;GAKG;AACH,MAAM,WAAW,mBAAmB;IAClC;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE;QACP;;;;WAIG;QACH,cAAc,EAAE,MAAM,CAAC;KACxB,CAAC;CACH;AAED;;GAEG;AACH,MAAM,WAAW,oBAAoB,CAAC,OAAO,SAAS,gBAAgB;IACpE,OAAO,EAAE,OAAO,CAAC;IACjB,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,IAAI,EAAE,OAAO,CAAC;CACf;AAED;;;;;GAKG;AACH,MAAM,CAAC,OAAO,CAAC,QAAQ,OAAO,UAAU,CACtC,MAAM,SAAS,eAAe,EAC9B,OAAO,SAAS,gBAAgB;IAEhC;;OAEG;IACH,SAAS,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC;IAEvB,SAAS,CAAC,YAAY,EAAE,MAAM,CAAC;IAE/B,SAAS;IAIT;;;;OAIG;IACH,MAAM,CAAC,GAAG,EAAE,MAAM;IAIlB;;;;;;;;OAQG;IACG,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,oBAAoB,CAAC,OAAO,CAAC,CAAC;IAgBjE;;;;;OAKG;YACW,cAAc;CAO7B"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { BSON } from 'bson';
|
|
2
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
3
|
+
import DOFunction, {
|
|
4
|
+
DOFunctionCallOutput,
|
|
5
|
+
DOFunctionInput,
|
|
6
|
+
DOFunctionOutput
|
|
7
|
+
} from './DOFunction.js';
|
|
8
|
+
|
|
9
|
+
describe('DOFunction', () => {
|
|
10
|
+
it('should call the function and return the expected output', async () => {
|
|
11
|
+
const exampleFunction = ExampleFunction.getFunction();
|
|
12
|
+
|
|
13
|
+
const input: ExampleInput = { name: 'John' };
|
|
14
|
+
const expectedOutput: DOFunctionCallOutput<ExampleOutput> = {
|
|
15
|
+
success: true,
|
|
16
|
+
errors: [],
|
|
17
|
+
data: { greeting: 'Hello, John!' }
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
global.fetch = vi.fn().mockResolvedValue({
|
|
21
|
+
text: vi
|
|
22
|
+
.fn()
|
|
23
|
+
.mockResolvedValue(
|
|
24
|
+
Buffer.from(BSON.serialize(expectedOutput)).toString('base64')
|
|
25
|
+
)
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
const output = await exampleFunction.call(input);
|
|
29
|
+
|
|
30
|
+
expect(output).toEqual(expectedOutput);
|
|
31
|
+
expect(global.fetch).toHaveBeenCalledWith(EXAMPLE_FUNCTION_URL, {
|
|
32
|
+
method: 'POST',
|
|
33
|
+
headers: {
|
|
34
|
+
Connection: 'keep-alive',
|
|
35
|
+
'Content-Type': 'application/octet-stream'
|
|
36
|
+
},
|
|
37
|
+
body: Buffer.from(BSON.serialize(input)).toString('base64')
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const EXAMPLE_FUNCTION_URL = 'https://example.com/function';
|
|
43
|
+
|
|
44
|
+
export class ExampleFunction extends DOFunction<ExampleInput, ExampleOutput> {
|
|
45
|
+
private static instance: ExampleFunction | undefined;
|
|
46
|
+
|
|
47
|
+
private constructor() {
|
|
48
|
+
super();
|
|
49
|
+
this.url = EXAMPLE_FUNCTION_URL;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
static getFunction(): ExampleFunction {
|
|
53
|
+
if (!ExampleFunction.instance) {
|
|
54
|
+
ExampleFunction.instance = new ExampleFunction();
|
|
55
|
+
}
|
|
56
|
+
return ExampleFunction.instance;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
interface ExampleInput extends DOFunctionInput {
|
|
61
|
+
name: string;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
interface ExampleOutput extends DOFunctionOutput {
|
|
65
|
+
greeting: string;
|
|
66
|
+
}
|
|
@@ -23,25 +23,25 @@ export interface DOFunctionRawInput {
|
|
|
23
23
|
*/
|
|
24
24
|
body: string;
|
|
25
25
|
headers: {
|
|
26
|
-
accept
|
|
27
|
-
'accept-encoding'
|
|
26
|
+
accept?: string;
|
|
27
|
+
'accept-encoding'?: string;
|
|
28
28
|
/**
|
|
29
29
|
* This is important.
|
|
30
30
|
*/
|
|
31
|
-
'content-type'
|
|
32
|
-
host
|
|
33
|
-
'user-agent'
|
|
34
|
-
'x-forwarded-for'
|
|
35
|
-
'x-forwarded-proto'
|
|
36
|
-
'x-request-id'
|
|
31
|
+
'content-type'?: string;
|
|
32
|
+
host?: string;
|
|
33
|
+
'user-agent'?: string;
|
|
34
|
+
'x-forwarded-for'?: string;
|
|
35
|
+
'x-forwarded-proto'?: string;
|
|
36
|
+
'x-request-id'?: string;
|
|
37
37
|
};
|
|
38
38
|
/**
|
|
39
39
|
* This needs to be used to determine if the body is base64 encoded.
|
|
40
40
|
*/
|
|
41
41
|
isBase64Encoded: boolean;
|
|
42
|
-
method
|
|
43
|
-
path
|
|
44
|
-
queryString
|
|
42
|
+
method?: string;
|
|
43
|
+
path?: string;
|
|
44
|
+
queryString?: string;
|
|
45
45
|
};
|
|
46
46
|
}
|
|
47
47
|
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { BSON } from 'bson';
|
|
2
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
3
|
+
import { DOFunctionRawInput, DOFunctionRawOutput } from './DOFunction.js';
|
|
4
|
+
import { ExampleFunction } from './DOFunction.spec.js';
|
|
5
|
+
import DOFunctionService from './DOFunctionService.js';
|
|
6
|
+
|
|
7
|
+
describe('DOFunctionService', () => {
|
|
8
|
+
it('should handle API request and return the expected raw output', async () => {
|
|
9
|
+
const rawInput: DOFunctionRawInput = {
|
|
10
|
+
http: {
|
|
11
|
+
body: Buffer.from(BSON.serialize({ name: 'John' })).toString('base64'),
|
|
12
|
+
headers: {
|
|
13
|
+
accept: '',
|
|
14
|
+
'accept-encoding': '',
|
|
15
|
+
'content-type': 'application/octet-stream',
|
|
16
|
+
host: '',
|
|
17
|
+
'user-agent': '',
|
|
18
|
+
'x-forwarded-for': '',
|
|
19
|
+
'x-forwarded-proto': '',
|
|
20
|
+
'x-request-id': ''
|
|
21
|
+
},
|
|
22
|
+
isBase64Encoded: true,
|
|
23
|
+
method: 'POST',
|
|
24
|
+
path: '',
|
|
25
|
+
queryString: ''
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const expectedOutput = {
|
|
30
|
+
success: true,
|
|
31
|
+
errors: [],
|
|
32
|
+
data: { greeting: 'Hello, John!' }
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const expectedRawOutput: DOFunctionRawOutput = {
|
|
36
|
+
body: Buffer.from(BSON.serialize(expectedOutput)).toString('base64'),
|
|
37
|
+
statusCode: 200,
|
|
38
|
+
headers: {
|
|
39
|
+
'Content-Type': 'application/octet-stream'
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
global.fetch = vi.fn().mockResolvedValue({
|
|
44
|
+
text: vi
|
|
45
|
+
.fn()
|
|
46
|
+
.mockResolvedValue(
|
|
47
|
+
Buffer.from(BSON.serialize(expectedOutput)).toString('base64')
|
|
48
|
+
)
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const handler = ExampleFunction.getFunction().call.bind(
|
|
52
|
+
ExampleFunction.getFunction()
|
|
53
|
+
);
|
|
54
|
+
const rawOutput = await DOFunctionService.handleApiRequest(
|
|
55
|
+
rawInput,
|
|
56
|
+
handler
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
expect(rawOutput).toEqual(expectedRawOutput);
|
|
60
|
+
});
|
|
61
|
+
});
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@aneuhold/core-ts-api-lib",
|
|
3
3
|
"author": "Anton G. Neuhold Jr.",
|
|
4
4
|
"license": "MIT",
|
|
5
|
-
"version": "2.0.
|
|
5
|
+
"version": "2.0.5",
|
|
6
6
|
"description": "A library for interacting with the backend and defining the backend API for personal projects.",
|
|
7
7
|
"packageManager": "yarn@4.5.1",
|
|
8
8
|
"type": "module",
|