-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostman-publish-lib.ts
More file actions
251 lines (227 loc) · 6.31 KB
/
Copy pathpostman-publish-lib.ts
File metadata and controls
251 lines (227 loc) · 6.31 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import crypto from 'crypto';
import fs from 'fs';
import path from 'path';
export const POSTMAN_API_BASE = 'https://api.getpostman.com';
export const WORKSPACE_NAME = 'Extole API';
export const WORKSPACE_SLUG = 'extole-api';
export const MAX_COLLECTION_BYTES = 30 * 1024 * 1024;
export interface CollectionMapping {
uid: string;
name: string;
}
export interface WorkspaceMapping {
id: string;
name: string;
slug: string;
teamDomain: string;
type: 'public' | 'team' | 'personal';
overviewUrl: string;
}
export interface PublishMapping {
workspace: WorkspaceMapping | null;
collections: Record<string, CollectionMapping>;
}
export interface PostmanCollectionItem {
name?: string;
request?: { method?: string; url?: unknown };
item?: PostmanCollectionItem[];
}
export interface PostmanCollection {
info: {
name: string;
description?: string | { content?: string };
schema?: string;
_postman_id?: string;
};
auth?: unknown;
variable?: Array<{ key: string; value: string }>;
item?: PostmanCollectionItem[];
}
const repoRoot = path.join(__dirname, '..');
export const postmanDir = path.join(repoRoot, 'postman');
export const mappingPath = path.join(postmanDir, '.postman-publish.json');
export function requireApiKey(): string {
const apiKey = process.env.POSTMAN_API_KEY?.trim();
if (!apiKey) {
console.error('POSTMAN_API_KEY is required.');
process.exit(1);
}
return apiKey;
}
export function readMapping(): PublishMapping {
if (!fs.existsSync(mappingPath)) {
return { workspace: null, collections: {} };
}
return JSON.parse(fs.readFileSync(mappingPath, 'utf8')) as PublishMapping;
}
export function writeMapping(mapping: PublishMapping): void {
fs.writeFileSync(mappingPath, `${JSON.stringify(mapping, null, 2)}\n`);
}
export function listCollectionFiles(): string[] {
return fs
.readdirSync(postmanDir)
.filter(
(filename) => filename.endsWith('.json') && !filename.startsWith('.'),
)
.sort();
}
export function loadLocalCollection(filename: string): PostmanCollection {
return JSON.parse(
fs.readFileSync(path.join(postmanDir, filename), 'utf8'),
) as PostmanCollection;
}
export async function deleteCollection(
apiKey: string,
uid: string,
): Promise<void> {
const response = await postmanFetch(apiKey, `/collections/${uid}`, {
method: 'DELETE',
});
const body = await readJson(response);
if (response.status === 404) {
return;
}
assertOk(response, body, `DELETE /collections/${uid}`);
}
export function collectionKeyFromFilename(filename: string): string {
return path.basename(filename, '.json');
}
export async function postmanFetch(
apiKey: string,
pathname: string,
init: RequestInit = {},
): Promise<Response> {
const headers = new Headers(init.headers);
headers.set('X-Api-Key', apiKey);
if (init.body && !headers.has('Content-Type')) {
headers.set('Content-Type', 'application/json');
}
return fetch(`${POSTMAN_API_BASE}${pathname}`, { ...init, headers });
}
export async function readJson(response: Response): Promise<unknown> {
const text = await response.text();
try {
return text ? JSON.parse(text) : {};
} catch {
throw new Error(
`Invalid JSON from Postman API (${response.status}): ${text.slice(0, 500)}`,
);
}
}
export function assertOk(
response: Response,
body: unknown,
context: string,
): void {
if (response.ok) {
return;
}
throw new Error(
`${context} failed (${response.status}): ${JSON.stringify(body).slice(0, 1000)}`,
);
}
export function workspaceOverviewUrl(teamDomain: string, slug: string): string {
return `https://www.postman.com/${teamDomain}/${slug}/overview`;
}
export function countRequests(
items: PostmanCollectionItem[] | undefined,
): number {
if (!items) {
return 0;
}
let count = 0;
for (const item of items) {
if (item.request) {
count += 1;
}
if (item.item) {
count += countRequests(item.item);
}
}
return count;
}
export function collectRequestPaths(
items: PostmanCollectionItem[] | undefined,
): string[] {
if (!items) {
return [];
}
const paths: string[] = [];
for (const item of items) {
if (item.request) {
const url = item.request.url as
| { path?: string[]; raw?: string }
| string
| undefined;
if (typeof url === 'string') {
paths.push(`${item.request.method ?? 'GET'} ${url}`);
} else if (url?.path) {
paths.push(`${item.request.method ?? 'GET'} /${url.path.join('/')}`);
} else if (url?.raw) {
paths.push(`${item.request.method ?? 'GET'} ${url.raw}`);
} else {
paths.push(`${item.request.method ?? 'GET'} ${item.name ?? 'unknown'}`);
}
}
if (item.item) {
paths.push(...collectRequestPaths(item.item));
}
}
return paths.sort();
}
export function normalizeDescription(
description: PostmanCollection['info']['description'],
): string {
if (!description) {
return '';
}
if (typeof description === 'string') {
return description;
}
return description.content ?? '';
}
export function fingerprintCollection(collection: PostmanCollection): string {
const payload = {
info: {
name: collection.info.name,
description: normalizeDescription(collection.info.description),
schema: collection.info.schema ?? '',
},
paths: collectRequestPaths(collection.item),
};
return crypto
.createHash('sha256')
.update(JSON.stringify(payload))
.digest('hex');
}
export function collectBaseUrls(collection: PostmanCollection): string[] {
const urls = new Set<string>();
for (const variable of collection.variable ?? []) {
if (variable.key === 'baseUrl') {
urls.add(variable.value);
}
}
return [...urls];
}
export function hasRequestMatching(
items: PostmanCollectionItem[] | undefined,
predicate: (method: string, path: string) => boolean,
): boolean {
if (!items) {
return false;
}
for (const item of items) {
if (item.request) {
const url = item.request.url as { path?: string[] } | undefined;
const method = item.request.method ?? 'GET';
const requestPath = url?.path ? `/${url.path.join('/')}` : '';
if (predicate(method, requestPath)) {
return true;
}
}
if (item.item && hasRequestMatching(item.item, predicate)) {
return true;
}
}
return false;
}