-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapi.ts
executable file
Β·136 lines (120 loc) Β· 4.58 KB
/
api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import Session from "../../../session/Session";
import fetch, { Headers } from "node-fetch";
import OrganizationsResponse from "../barrel/model/OrganizationsResponse";
import ProjectsResponse from "../barrel/model/ProjectsResponse";
import StyleguidesResponse from "../barrel/model/StyleguidesResponse";
import ProjectDetailsResponse from "../barrel/model/ProjectDetailsResponse";
import StyleguideDetailsResponse from "../barrel/model/StyleguideDetailsResponse";
import BaseError from "../error/BaseError";
import BarrelType from "../barrel/BarrelType";
import configuration from "../extension/configuration";
import Logger from "../../../log/Logger";
import ScreensResponse from "../../../sidebar/screen/model/ScreensResponse";
const HEADER_KEY_CONTENT_TYPE = "Content-Type";
const HEADER_KEY_ACCESS_TOKEN = "Zeplin-Access-Token";
const HEADER_KEY_CHILD_PROJECT = "Zeplin-Project-Id";
const HEADER_KEY_CHILD_STYLEGUIDE = "Zeplin-Styleguide-Id";
const HEADER_VALUE_CONTENT_TYPE = "application/json";
const METHOD_GET = "GET";
type ApiResult<T> = Promise<T | BaseError>;
interface ApiError {
title: string;
message: string;
}
type HeaderContainer = { [key: string]: string };
async function get<T>(path: string, extraHeaders: HeaderContainer = {}): Promise<T | BaseError> {
try {
Logger.log(`fetching ${path}`);
const headers = new Headers({
[HEADER_KEY_CONTENT_TYPE]: HEADER_VALUE_CONTENT_TYPE,
[HEADER_KEY_ACCESS_TOKEN]: (await Session.getToken())!,
...extraHeaders
});
const response = await fetch(configuration.apiUrl + path, {
method: METHOD_GET,
headers
});
if (!response.ok) {
const error = await response.json() as ApiError;
Logger.log(`failed ${path} Code: ${response.status} Title: ${error.title} Message: ${error.message}`);
return new BaseError(error.title ?? error.message, response.status);
} else {
const success = await response.json() as T;
Logger.log(`fetched: ${path}`);
return success;
}
} catch (error) {
Logger.error(`fetching ${path} failed`, error);
return new BaseError();
}
}
/**
* Fetches user's organizations.
*/
function getOrganizations(): ApiResult<OrganizationsResponse> {
return get("/public/vscodeextension/organizations");
}
/**
* Fetches an organization's projects.
* @param organizationId An organization id.
*/
function getOrganizationProjects(organizationId: string): ApiResult<ProjectsResponse> {
return get(`/public/vscodeextension/organizations/${organizationId}/projects`);
}
/**
* Fetches an organization's styleguides.
* @param organizationId An organization id.
*/
function getOrganizationStyleguides(organizationId: string): ApiResult<StyleguidesResponse> {
return get(`/public/vscodeextension/organizations/${organizationId}/styleguides`);
}
/**
* Fetches user's personal projects.
*/
function getPersonalProjects(): ApiResult<ProjectsResponse> {
return get(`/public/vscodeextension/projects`);
}
/**
* Fetches user's personal styleguides.
*/
function getPersonalStyleguides(): ApiResult<StyleguidesResponse> {
return get(`/public/vscodeextension/styleguides`);
}
/**
* Fetches a project's details.
* @param projectId A project id.
*/
function getProjectDetails(projectId: string): ApiResult<ProjectDetailsResponse> {
return get(`/public/vscodeextension/projects/${projectId}`);
}
/**
* Fetches a styleguide's details.
* Note: childId and childType can be provided to get styleguide's details even if the user has not joined it.
* @param styleguideId A styleguide id.
* @param childId Id of a known child of the styleguide whose details are requested.
* @param childType Barrel type of a known child of the styleguide whose details are requested.
*/
function getStyleguideDetails(styleguideId: string, childId?: string, childType?: BarrelType):
ApiResult<StyleguideDetailsResponse> {
const headers = childId ? {
[childType === BarrelType.Project ? HEADER_KEY_CHILD_PROJECT : HEADER_KEY_CHILD_STYLEGUIDE]: childId
} : {};
return get(`/public/vscodeextension/styleguides/${styleguideId}`, headers);
}
/**
* Fetches a project's screens.
* @param projectId A project id
*/
function getScreens(projectId: string): ApiResult<ScreensResponse> {
return get(`/public/vscodeextension/projects/${projectId}/screens`);
}
export {
getOrganizations,
getOrganizationProjects,
getOrganizationStyleguides,
getPersonalProjects,
getPersonalStyleguides,
getProjectDetails,
getStyleguideDetails,
getScreens
};