Skip to content

Commit 2e34994

Browse files
committed
Improved the API and tested it further.
1 parent e162b9f commit 2e34994

2 files changed

Lines changed: 98 additions & 21 deletions

File tree

src/protocol.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export enum ResourceType {
5454
Repository = 'Repository'
5555
}
5656

57-
export interface AddResponse {
57+
export interface Resource {
5858
id: string;
5959
}
6060

@@ -111,12 +111,8 @@ export interface API {
111111
blob: unknown,
112112
jsons?: MetaCallJSON[],
113113
runners?: string[]
114-
): Promise<string>;
115-
add(
116-
url: string,
117-
branch: string,
118-
jsons: MetaCallJSON[]
119-
): Promise<AddResponse>;
114+
): Promise<Resource>;
115+
add(url: string, branch: string, jsons: MetaCallJSON[]): Promise<Resource>;
120116
deploy(
121117
name: string,
122118
env: { name: string; value: string }[],
@@ -262,7 +258,7 @@ class Request {
262258
}
263259

264260
export default (token: string, baseURL: string): API => {
265-
const request = () => new Request(token, baseURL);
261+
const request = (url = baseURL) => new Request(token, url);
266262

267263
const api: API = {
268264
refresh: (): Promise<string> =>
@@ -323,7 +319,7 @@ export default (token: string, baseURL: string): API => {
323319
blob: Blob,
324320
jsons: MetaCallJSON[] = [],
325321
runners: string[] = []
326-
): Promise<string> => {
322+
): Promise<Resource> => {
327323
const fd = new FormData();
328324

329325
fd.append('id', name);
@@ -336,14 +332,14 @@ export default (token: string, baseURL: string): API => {
336332
.url('/api/package/create')
337333
.method('POST')
338334
.blob(fd)
339-
.asJson<string>();
335+
.asJson<Resource>();
340336
},
341337

342338
add: (
343339
url: string,
344340
branch: string,
345341
jsons: MetaCallJSON[] = []
346-
): Promise<AddResponse> =>
342+
): Promise<Resource> =>
347343
request()
348344
.url('/api/repository/add')
349345
.method('POST')
@@ -352,7 +348,7 @@ export default (token: string, baseURL: string): API => {
352348
branch,
353349
jsons
354350
})
355-
.asJson<AddResponse>(),
351+
.asJson<Resource>(),
356352

357353
branchList: (url: string): Promise<Branches> =>
358354
request()
@@ -437,9 +433,15 @@ export default (token: string, baseURL: string): API => {
437433
name: string,
438434
args?: Args
439435
): Promise<Result> => {
440-
const req = request().url(
441-
`/${prefix}/${suffix}/${version}/${type}/${name}`
442-
);
436+
// Old API
437+
// const req = request('https://api.metacall.io').url(
438+
// `/${prefix}/${suffix}/${version}/${type}/${name}`
439+
// );
440+
441+
// New API
442+
const req = request(
443+
`https://${version}-${suffix}-${prefix}.api.metacall.io`
444+
).url(`/${type}/${name}`);
443445

444446
if (args === undefined) {
445447
req.method('GET');

src/test/api.integration.spec.ts

Lines changed: 81 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { strictEqual } from 'assert';
2+
import { readFile } from 'fs/promises';
3+
import { join } from 'path';
24
import { Plans } from '../plan';
35
import Protocol, { ResourceType, waitFor } from '../protocol';
46

@@ -10,12 +12,15 @@ describe('Integration API', function () {
1012

1113
if (token === undefined) {
1214
// Skip test
15+
console.warn(
16+
'⚠️ Warning: Integration API Test being skipped due to API_TOKEN not defined'
17+
);
1318
return;
1419
}
1520

1621
const API = Protocol(token, baseURL);
1722

18-
it('Should add repository', async function () {
23+
it.skip('Should deploy a repository', async function () {
1924
const { id } = await API.add(
2025
'https://github.com/metacall/examples.git',
2126
'master',
@@ -31,9 +36,6 @@ describe('Integration API', function () {
3136
'v1'
3237
);
3338

34-
console.log(JSON.stringify(resource, null, 2));
35-
console.log(resource.suffix);
36-
3739
const deploy = await waitFor(async cancel => {
3840
const deploy = await API.inspectByName(resource.suffix);
3941

@@ -46,9 +48,82 @@ describe('Integration API', function () {
4648
return deploy;
4749
});
4850

49-
console.log(JSON.stringify(deploy, null, 2));
50-
5151
strictEqual(deploy.suffix, 'metacall-examples');
5252
strictEqual(deploy.status, 'ready');
53+
54+
const result = await API.deployDelete(
55+
deploy.prefix,
56+
deploy.suffix,
57+
deploy.version
58+
);
59+
60+
strictEqual(result, 'Deploy Delete Succeed');
61+
});
62+
63+
it.skip('Should deploy a package', async function () {
64+
const basePath = join(
65+
process.cwd(),
66+
'src',
67+
'test',
68+
'resources',
69+
'integration',
70+
'api'
71+
);
72+
73+
const fileBuffer = await readFile(
74+
join(basePath, 'metacall-protocol-package-test.zip')
75+
);
76+
77+
const blob = new Blob([fileBuffer], { type: 'application/zip' });
78+
79+
const { id } = await API.upload(
80+
'metacall-protocol-package-test',
81+
blob,
82+
[],
83+
['nodejs']
84+
);
85+
86+
strictEqual(id, 'metacall-protocol-package-test');
87+
88+
const resource = await API.deploy(
89+
id,
90+
[],
91+
Plans.Essential,
92+
ResourceType.Package,
93+
'19d499ed1ab',
94+
'v1'
95+
);
96+
97+
const deploy = await waitFor(async cancel => {
98+
const deploy = await API.inspectByName(resource.suffix);
99+
100+
if (deploy.status === 'create') {
101+
throw new Error('Not ready yet');
102+
} else if (deploy.status === 'fail') {
103+
cancel('Deploy failed');
104+
}
105+
106+
return deploy;
107+
});
108+
109+
strictEqual(deploy.suffix, 'metacall-protocol-package-test');
110+
strictEqual(deploy.status, 'ready');
111+
112+
const value = await API.call<string>(
113+
deploy.prefix,
114+
deploy.suffix,
115+
deploy.version,
116+
'test'
117+
);
118+
119+
strictEqual(value, 'OK');
120+
121+
const result = await API.deployDelete(
122+
deploy.prefix,
123+
deploy.suffix,
124+
deploy.version
125+
);
126+
127+
strictEqual(result, 'Deploy Delete Succeed');
53128
});
54129
});

0 commit comments

Comments
 (0)