@aneuhold/core-ts-api-lib 2.0.8 → 2.0.10
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.js +3 -3
- package/lib/services/DOFunctionService/DOFunction.js.map +1 -1
- package/lib/services/DOFunctionService/DOFunction.ts +3 -3
- package/lib/tests/ExampleFunction.d.ts +14 -0
- package/lib/tests/ExampleFunction.d.ts.map +1 -0
- package/lib/tests/ExampleFunction.js +16 -0
- package/lib/tests/ExampleFunction.js.map +1 -0
- package/lib/tests/ExampleFunction.ts +30 -0
- package/lib/tests/TestUtil.d.ts +10 -0
- package/lib/tests/TestUtil.d.ts.map +1 -0
- package/lib/tests/TestUtil.js +19 -0
- package/lib/tests/TestUtil.js.map +1 -0
- package/lib/tests/TestUtil.ts +19 -0
- package/lib/tests/services/DOFunctionService/DOFunction.spec.ts +37 -0
- package/package.json +2 -2
- package/lib/services/DOFunctionService/DOFunction.spec.ts +0 -66
- package/lib/services/DOFunctionService/DOFunctionService.spec.ts +0 -61
|
@@ -56,9 +56,9 @@ export default class DOFunction {
|
|
|
56
56
|
async decodeResponse(response) {
|
|
57
57
|
const isBson = response.headers.get('Content-Type') === 'application/octet-stream';
|
|
58
58
|
if (isBson) {
|
|
59
|
-
const
|
|
60
|
-
const
|
|
61
|
-
return BSON.deserialize(
|
|
59
|
+
const buffer = await response.arrayBuffer();
|
|
60
|
+
const uint8Array = new Uint8Array(buffer);
|
|
61
|
+
return BSON.deserialize(uint8Array);
|
|
62
62
|
}
|
|
63
63
|
else {
|
|
64
64
|
// This normally only happens if there is an error
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DOFunction.js","sourceRoot":"./src/","sources":["services/DOFunctionService/DOFunction.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AA8E5B;;;;;GAKG;AACH,MAAM,CAAC,OAAO,OAAgB,UAAU;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AAC,CAAC,CAAC;gBACzC,IAAI,EAAE,EAAa;aACpB,CAAC;QACJ,CAAC;IACH,CAAC;CACF"}
|
|
@@ -144,9 +144,9 @@ export default abstract class DOFunction<
|
|
|
144
144
|
const isBson =
|
|
145
145
|
response.headers.get('Content-Type') === 'application/octet-stream';
|
|
146
146
|
if (isBson) {
|
|
147
|
-
const
|
|
148
|
-
const
|
|
149
|
-
return BSON.deserialize(
|
|
147
|
+
const buffer = await response.arrayBuffer();
|
|
148
|
+
const uint8Array = new Uint8Array(buffer);
|
|
149
|
+
return BSON.deserialize(uint8Array) as DOFunctionCallOutput<TOutput>;
|
|
150
150
|
} else {
|
|
151
151
|
// This normally only happens if there is an error
|
|
152
152
|
const result = (await response.json()) as unknown;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import DOFunction, { DOFunctionInput, DOFunctionOutput } from '../services/DOFunctionService/DOFunction.js';
|
|
2
|
+
export declare const EXAMPLE_FUNCTION_URL = "https://example.com/function";
|
|
3
|
+
export declare class ExampleFunction extends DOFunction<ExampleInput, ExampleOutput> {
|
|
4
|
+
private static instance;
|
|
5
|
+
private constructor();
|
|
6
|
+
static getFunction(): ExampleFunction;
|
|
7
|
+
}
|
|
8
|
+
export interface ExampleInput extends DOFunctionInput {
|
|
9
|
+
name: string;
|
|
10
|
+
}
|
|
11
|
+
export interface ExampleOutput extends DOFunctionOutput {
|
|
12
|
+
greeting: string;
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=ExampleFunction.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ExampleFunction.d.ts","sourceRoot":"./src/","sources":["tests/ExampleFunction.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,EAAE,EACjB,eAAe,EACf,gBAAgB,EACjB,MAAM,6CAA6C,CAAC;AAErD,eAAO,MAAM,oBAAoB,iCAAiC,CAAC;AAEnE,qBAAa,eAAgB,SAAQ,UAAU,CAAC,YAAY,EAAE,aAAa,CAAC;IAC1E,OAAO,CAAC,MAAM,CAAC,QAAQ,CAA8B;IAErD,OAAO;IAKP,MAAM,CAAC,WAAW,IAAI,eAAe;CAMtC;AAED,MAAM,WAAW,YAAa,SAAQ,eAAe;IACnD,IAAI,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,aAAc,SAAQ,gBAAgB;IACrD,QAAQ,EAAE,MAAM,CAAC;CAClB"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import DOFunction from '../services/DOFunctionService/DOFunction.js';
|
|
2
|
+
export const EXAMPLE_FUNCTION_URL = 'https://example.com/function';
|
|
3
|
+
export class ExampleFunction extends DOFunction {
|
|
4
|
+
static instance;
|
|
5
|
+
constructor() {
|
|
6
|
+
super();
|
|
7
|
+
this.url = EXAMPLE_FUNCTION_URL;
|
|
8
|
+
}
|
|
9
|
+
static getFunction() {
|
|
10
|
+
if (!ExampleFunction.instance) {
|
|
11
|
+
ExampleFunction.instance = new ExampleFunction();
|
|
12
|
+
}
|
|
13
|
+
return ExampleFunction.instance;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=ExampleFunction.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ExampleFunction.js","sourceRoot":"./src/","sources":["tests/ExampleFunction.ts"],"names":[],"mappings":"AAAA,OAAO,UAGN,MAAM,6CAA6C,CAAC;AAErD,MAAM,CAAC,MAAM,oBAAoB,GAAG,8BAA8B,CAAC;AAEnE,MAAM,OAAO,eAAgB,SAAQ,UAAuC;IAClE,MAAM,CAAC,QAAQ,CAA8B;IAErD;QACE,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,GAAG,GAAG,oBAAoB,CAAC;IAClC,CAAC;IAED,MAAM,CAAC,WAAW;QAChB,IAAI,CAAC,eAAe,CAAC,QAAQ,EAAE,CAAC;YAC9B,eAAe,CAAC,QAAQ,GAAG,IAAI,eAAe,EAAE,CAAC;QACnD,CAAC;QACD,OAAO,eAAe,CAAC,QAAQ,CAAC;IAClC,CAAC;CACF"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import DOFunction, {
|
|
2
|
+
DOFunctionInput,
|
|
3
|
+
DOFunctionOutput
|
|
4
|
+
} from '../services/DOFunctionService/DOFunction.js';
|
|
5
|
+
|
|
6
|
+
export const EXAMPLE_FUNCTION_URL = 'https://example.com/function';
|
|
7
|
+
|
|
8
|
+
export class ExampleFunction extends DOFunction<ExampleInput, ExampleOutput> {
|
|
9
|
+
private static instance: ExampleFunction | undefined;
|
|
10
|
+
|
|
11
|
+
private constructor() {
|
|
12
|
+
super();
|
|
13
|
+
this.url = EXAMPLE_FUNCTION_URL;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
static getFunction(): ExampleFunction {
|
|
17
|
+
if (!ExampleFunction.instance) {
|
|
18
|
+
ExampleFunction.instance = new ExampleFunction();
|
|
19
|
+
}
|
|
20
|
+
return ExampleFunction.instance;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface ExampleInput extends DOFunctionInput {
|
|
25
|
+
name: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface ExampleOutput extends DOFunctionOutput {
|
|
29
|
+
greeting: string;
|
|
30
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare class TestUtil {
|
|
2
|
+
/**
|
|
3
|
+
* Mocks fetch globally to return the expected output. This should match how
|
|
4
|
+
* the output is expected to be returned from the Digital Ocean function.
|
|
5
|
+
*
|
|
6
|
+
* @param expectedOutput The expected output from the fetch call.
|
|
7
|
+
*/
|
|
8
|
+
static mockFetch(expectedOutput: object): void;
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=TestUtil.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TestUtil.d.ts","sourceRoot":"./src/","sources":["tests/TestUtil.ts"],"names":[],"mappings":"AAGA,qBAAa,QAAQ;IACnB;;;;;OAKG;IACH,MAAM,CAAC,SAAS,CAAC,cAAc,EAAE,MAAM;CAQxC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { BSON } from 'bson';
|
|
2
|
+
import { vi } from 'vitest';
|
|
3
|
+
export class TestUtil {
|
|
4
|
+
/**
|
|
5
|
+
* Mocks fetch globally to return the expected output. This should match how
|
|
6
|
+
* the output is expected to be returned from the Digital Ocean function.
|
|
7
|
+
*
|
|
8
|
+
* @param expectedOutput The expected output from the fetch call.
|
|
9
|
+
*/
|
|
10
|
+
static mockFetch(expectedOutput) {
|
|
11
|
+
global.fetch = vi.fn().mockResolvedValue({
|
|
12
|
+
arrayBuffer: vi
|
|
13
|
+
.fn()
|
|
14
|
+
.mockResolvedValue(Buffer.from(BSON.serialize(expectedOutput))),
|
|
15
|
+
headers: new Map([['Content-Type', 'application/octet-stream']])
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=TestUtil.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TestUtil.js","sourceRoot":"./src/","sources":["tests/TestUtil.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAC5B,OAAO,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AAE5B,MAAM,OAAO,QAAQ;IACnB;;;;;OAKG;IACH,MAAM,CAAC,SAAS,CAAC,cAAsB;QACrC,MAAM,CAAC,KAAK,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC,iBAAiB,CAAC;YACvC,WAAW,EAAE,EAAE;iBACZ,EAAE,EAAE;iBACJ,iBAAiB,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,cAAc,CAAC,CAAC,CAAC;YACjE,OAAO,EAAE,IAAI,GAAG,CAAC,CAAC,CAAC,cAAc,EAAE,0BAA0B,CAAC,CAAC,CAAC;SACjE,CAAC,CAAC;IACL,CAAC;CACF"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { BSON } from 'bson';
|
|
2
|
+
import { vi } from 'vitest';
|
|
3
|
+
|
|
4
|
+
export class TestUtil {
|
|
5
|
+
/**
|
|
6
|
+
* Mocks fetch globally to return the expected output. This should match how
|
|
7
|
+
* the output is expected to be returned from the Digital Ocean function.
|
|
8
|
+
*
|
|
9
|
+
* @param expectedOutput The expected output from the fetch call.
|
|
10
|
+
*/
|
|
11
|
+
static mockFetch(expectedOutput: object) {
|
|
12
|
+
global.fetch = vi.fn().mockResolvedValue({
|
|
13
|
+
arrayBuffer: vi
|
|
14
|
+
.fn()
|
|
15
|
+
.mockResolvedValue(Buffer.from(BSON.serialize(expectedOutput))),
|
|
16
|
+
headers: new Map([['Content-Type', 'application/octet-stream']])
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { BSON } from 'bson';
|
|
2
|
+
import { describe, expect, it } from 'vitest';
|
|
3
|
+
import { DOFunctionCallOutput } from '../../../services/DOFunctionService/DOFunction.js';
|
|
4
|
+
import {
|
|
5
|
+
EXAMPLE_FUNCTION_URL,
|
|
6
|
+
ExampleFunction,
|
|
7
|
+
ExampleInput,
|
|
8
|
+
ExampleOutput
|
|
9
|
+
} from '../../ExampleFunction.js';
|
|
10
|
+
import { TestUtil } from '../../TestUtil.js';
|
|
11
|
+
|
|
12
|
+
describe('DOFunction', () => {
|
|
13
|
+
it('should call the function and return the expected output', async () => {
|
|
14
|
+
const exampleFunction = ExampleFunction.getFunction();
|
|
15
|
+
|
|
16
|
+
const input: ExampleInput = { name: 'John' };
|
|
17
|
+
const expectedOutput: DOFunctionCallOutput<ExampleOutput> = {
|
|
18
|
+
success: true,
|
|
19
|
+
errors: [],
|
|
20
|
+
data: { greeting: 'Hello, John!' }
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
TestUtil.mockFetch(expectedOutput);
|
|
24
|
+
|
|
25
|
+
const output = await exampleFunction.call(input);
|
|
26
|
+
|
|
27
|
+
expect(output).toEqual(expectedOutput);
|
|
28
|
+
expect(global.fetch).toHaveBeenCalledWith(EXAMPLE_FUNCTION_URL, {
|
|
29
|
+
method: 'POST',
|
|
30
|
+
headers: {
|
|
31
|
+
Connection: 'keep-alive',
|
|
32
|
+
'Content-Type': 'application/octet-stream'
|
|
33
|
+
},
|
|
34
|
+
body: Buffer.from(BSON.serialize(input))
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
});
|
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.10",
|
|
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",
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"@aneuhold/main-scripts": "^2.0.6",
|
|
53
53
|
"@types/node": "^20.17.1",
|
|
54
54
|
"eslint": "^9.13.0",
|
|
55
|
-
"jsr": "^0.13.
|
|
55
|
+
"jsr": "^0.13.3",
|
|
56
56
|
"prettier": "^3.3.3",
|
|
57
57
|
"rimraf": "^6.0.1",
|
|
58
58
|
"tsx": "^4.19.1",
|
|
@@ -1,66 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,61 +0,0 @@
|
|
|
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
|
-
});
|