Skip to content

Commit 87695cd

Browse files
philnashdkundel
authored andcommitted
feat(logs): implements fetch and list of logs (#12)
This also consolidates some of the API return types to reduce duplication. It also implements the missing getService API method to fetch an individual service object.
1 parent b844e58 commit 87695cd

File tree

4 files changed

+98
-17
lines changed

4 files changed

+98
-17
lines changed

Diff for: src/api/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ export * from './environments';
77
export * from './functions';
88
export * from './services';
99
export * from './variables';
10+
export * from './logs';

Diff for: src/api/logs.ts

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/** @module @twilio-labs/serverless-api/dist/api */
2+
3+
import debug from 'debug';
4+
import { GotClient, LogApiResource, LogList, Sid } from '../types';
5+
6+
const log = debug('twilio-serverless-api:logs');
7+
8+
/**
9+
* Calls the API to retrieve a list of all assets
10+
*
11+
* @param {Sid} environmentSid environment in which to get logs
12+
* @param {Sid} serviceSid service to look for logs
13+
* @param {GotClient} client API client
14+
* @returns {Promise<LogApiResource[]>}
15+
*/
16+
export async function listLogResources(
17+
environmentSid: Sid,
18+
serviceSid: Sid,
19+
client: GotClient
20+
) {
21+
try {
22+
const resp = await client.get(
23+
`/Services/${serviceSid}/Environments/${environmentSid}/Logs`
24+
);
25+
const content = (resp.body as unknown) as LogList;
26+
return content.logs;
27+
} catch (err) {
28+
log('%O', err);
29+
throw err;
30+
}
31+
}
32+
33+
/**
34+
* Calls the API to retrieve a list of all assets
35+
*
36+
* @param {Sid} logSid SID of log to retrieve
37+
* @param {Sid} environmentSid environment in which to get logs
38+
* @param {Sid} serviceSid service to look for logs
39+
* @param {GotClient} client API client
40+
* @returns {Promise<LogApiResource>}
41+
*/
42+
export async function getLog(
43+
logSid: Sid,
44+
environmentSid: Sid,
45+
serviceSid: Sid,
46+
client: GotClient
47+
) {
48+
try {
49+
const resp = await client.get(
50+
`/Services/${serviceSid}/Environments/${environmentSid}/Logs/${logSid}`
51+
);
52+
return (resp.body as unknown) as LogApiResource;
53+
} catch (err) {
54+
log('%O', err);
55+
throw err;
56+
}
57+
}

Diff for: src/api/services.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/** @module @twilio-labs/serverless-api/dist/api */
22

33
import debug from 'debug';
4-
import { GotClient, ServiceList, ServiceResource } from '../types';
4+
import { GotClient, ServiceList, ServiceResource, Sid } from '../types';
55

66
const log = debug('twilio-serverless-api:services');
77

@@ -75,3 +75,16 @@ export async function findServiceSid(
7575
}
7676
return undefined;
7777
}
78+
79+
export async function getService(
80+
sid: Sid,
81+
client: GotClient
82+
): Promise<ServiceResource> {
83+
try {
84+
const resp = await client.get(`/Services/${sid}`);
85+
return (resp.body as unknown) as ServiceResource;
86+
} catch (err) {
87+
log('%O', err);
88+
throw err;
89+
}
90+
}

Diff for: src/types/serverless-api.ts

+26-16
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,38 @@ export interface ResourceBase {
88
sid: Sid;
99
account_sid: Sid;
1010
date_created: string;
11-
date_updated: string;
1211
url: string;
1312
}
1413

15-
export interface FunctionApiResource extends ResourceBase {
14+
export interface UpdateableResourceBase extends ResourceBase {
15+
date_updated: string;
16+
}
17+
18+
export interface FunctionApiResource extends UpdateableResourceBase {
1619
friendly_name: string;
1720
}
1821

1922
export interface FunctionList {
2023
functions: FunctionApiResource[];
2124
}
2225

23-
export interface AssetApiResource extends ResourceBase {
26+
export interface AssetApiResource extends UpdateableResourceBase {
2427
friendly_name: string;
2528
}
2629

2730
export interface AssetList {
2831
assets: AssetApiResource[];
2932
}
3033

31-
export interface ServiceResource extends ResourceBase {
34+
export interface ServiceResource extends UpdateableResourceBase {
3235
unique_name: string;
3336
}
3437

3538
export interface ServiceList {
3639
services: ServiceResource[];
3740
}
3841

39-
export interface EnvironmentResource extends ResourceBase {
42+
export interface EnvironmentResource extends UpdateableResourceBase {
4043
unique_name: string;
4144
domain_name: string;
4245
build_sid: string;
@@ -47,7 +50,7 @@ export interface EnvironmentList {
4750
environments: EnvironmentResource[];
4851
}
4952

50-
export interface VersionResource extends ResourceBase {
53+
export interface VersionResource extends UpdateableResourceBase {
5154
pre_signed_upload_url: {
5255
url: string;
5356
kmsARN: string;
@@ -56,12 +59,10 @@ export interface VersionResource extends ResourceBase {
5659

5760
export type BuildStatus = 'building' | 'completed' | 'failed';
5861

59-
export interface VersionOnBuild extends ResourceBase {
62+
export interface VersionOnBuild extends UpdateableResourceBase {
6063
path: string;
6164
visibility: 'public' | 'protected' | 'private';
62-
date_created: string;
6365
service_sid: string;
64-
account_sid: string;
6566
}
6667

6768
export interface FunctionVersion extends VersionOnBuild {
@@ -72,7 +73,7 @@ export interface AssetVersion extends VersionOnBuild {
7273
asset_sid: string;
7374
}
7475

75-
export interface BuildResource extends ResourceBase {
76+
export interface BuildResource extends UpdateableResourceBase {
7677
status: BuildStatus;
7778
function_versions: FunctionVersion[];
7879
asset_versions: AssetVersion[];
@@ -82,15 +83,10 @@ export interface BuildList {
8283
builds: BuildResource[];
8384
}
8485

85-
export interface VariableResource extends ResourceBase {
86-
date_updated: string;
86+
export interface VariableResource extends UpdateableResourceBase {
8787
environment_sid: string;
8888
value: string;
89-
account_sid: string;
90-
url: string;
9189
key: string;
92-
sid: string;
93-
date_created: string;
9490
service_sid: string;
9591
}
9692

@@ -103,3 +99,17 @@ export type BuildConfig = {
10399
functionVersions?: Sid[];
104100
assetVersions?: Sid[];
105101
};
102+
103+
export interface LogApiResource extends ResourceBase {
104+
service_sid: string;
105+
environment_sid: string;
106+
deployment_sid: string;
107+
function_sid: string;
108+
request_sid: string;
109+
level: string;
110+
message: string;
111+
}
112+
113+
export interface LogList {
114+
logs: LogApiResource[];
115+
}

0 commit comments

Comments
 (0)