@gitbook/api 0.0.0
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/dist/client.d.ts +319 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +296 -0
- package/package.json +25 -0
- package/spec/openapi.dereference.json +2648 -0
- package/spec/openapi.json +894 -0
- package/spec/openapi.yaml +570 -0
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
export interface Error {
|
|
2
|
+
/** @format int32 */
|
|
3
|
+
code: number;
|
|
4
|
+
message: string;
|
|
5
|
+
}
|
|
6
|
+
export interface RequestSpaceTrackPageView {
|
|
7
|
+
/** Unique identifier of the page */
|
|
8
|
+
pageId: string;
|
|
9
|
+
/** Unique identifier of the visitor */
|
|
10
|
+
visitorId: string;
|
|
11
|
+
}
|
|
12
|
+
export interface RequestPublishIntegration {
|
|
13
|
+
/** Base64 content of the icon */
|
|
14
|
+
icon?: string;
|
|
15
|
+
title: any;
|
|
16
|
+
description: any;
|
|
17
|
+
summary?: any;
|
|
18
|
+
visibility?: IntegrationVisibility;
|
|
19
|
+
scopes: any;
|
|
20
|
+
categories?: any;
|
|
21
|
+
configurations?: IntegrationConfigurations;
|
|
22
|
+
/** Content of the script to use */
|
|
23
|
+
script: string;
|
|
24
|
+
/** Secrets stored on the integration and passed at runtime. */
|
|
25
|
+
secrets?: IntegrationSecrets;
|
|
26
|
+
}
|
|
27
|
+
export interface RequestUpdateIntegrationInstallation {
|
|
28
|
+
status?: IntegrationInstallationStatus;
|
|
29
|
+
configurations?: any;
|
|
30
|
+
}
|
|
31
|
+
export interface User {
|
|
32
|
+
id: string;
|
|
33
|
+
displayName: string;
|
|
34
|
+
email?: string;
|
|
35
|
+
}
|
|
36
|
+
export interface Space {
|
|
37
|
+
/** Unique identifier for the space */
|
|
38
|
+
id: string;
|
|
39
|
+
/** Title of the space */
|
|
40
|
+
title: string;
|
|
41
|
+
}
|
|
42
|
+
export interface Integration {
|
|
43
|
+
/** Unique named identifier for the integration */
|
|
44
|
+
name: string;
|
|
45
|
+
/** Title of the integration */
|
|
46
|
+
title: string;
|
|
47
|
+
/** Title of the integration */
|
|
48
|
+
description?: string;
|
|
49
|
+
/** Long form markdown summary of the integration */
|
|
50
|
+
summary?: string;
|
|
51
|
+
/** Content of the script to run. */
|
|
52
|
+
script: string;
|
|
53
|
+
visibility: IntegrationVisibility;
|
|
54
|
+
/** Permissions that should be granted to the integration */
|
|
55
|
+
scopes: IntegrationScope[];
|
|
56
|
+
/** Categories for which the integration is listed in the marketplace */
|
|
57
|
+
categories: IntegrationCategory[];
|
|
58
|
+
configurations?: IntegrationConfigurations;
|
|
59
|
+
}
|
|
60
|
+
export declare enum IntegrationScope {
|
|
61
|
+
SpaceViews = "space:views",
|
|
62
|
+
SpaceContent = "space:content"
|
|
63
|
+
}
|
|
64
|
+
export declare enum IntegrationCategory {
|
|
65
|
+
Analytics = "analytics",
|
|
66
|
+
Collaboration = "collaboration",
|
|
67
|
+
Other = "other"
|
|
68
|
+
}
|
|
69
|
+
export interface IntegrationConfigurations {
|
|
70
|
+
space?: IntegrationConfiguration;
|
|
71
|
+
}
|
|
72
|
+
export interface IntegrationConfiguration {
|
|
73
|
+
properties: Record<string, {
|
|
74
|
+
type?: string;
|
|
75
|
+
title?: string;
|
|
76
|
+
description?: string;
|
|
77
|
+
} & ({
|
|
78
|
+
type?: "string";
|
|
79
|
+
default?: string;
|
|
80
|
+
} | {
|
|
81
|
+
type?: "number";
|
|
82
|
+
default?: number;
|
|
83
|
+
} | {
|
|
84
|
+
type?: "boolean";
|
|
85
|
+
default?: boolean;
|
|
86
|
+
} | {
|
|
87
|
+
type?: "button";
|
|
88
|
+
callback_url: string;
|
|
89
|
+
button_text: string;
|
|
90
|
+
})>;
|
|
91
|
+
}
|
|
92
|
+
export declare enum IntegrationVisibility {
|
|
93
|
+
Public = "public",
|
|
94
|
+
Private = "private"
|
|
95
|
+
}
|
|
96
|
+
export interface IntegrationInstallation {
|
|
97
|
+
id: string;
|
|
98
|
+
status: IntegrationInstallationStatus;
|
|
99
|
+
configurations?: {
|
|
100
|
+
space?: object;
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
export declare enum IntegrationInstallationStatus {
|
|
104
|
+
Active = "active",
|
|
105
|
+
Pending = "pending",
|
|
106
|
+
Paused = "paused"
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Secrets stored on the integration and passed at runtime.
|
|
110
|
+
*/
|
|
111
|
+
export declare type IntegrationSecrets = Record<string, string>;
|
|
112
|
+
/**
|
|
113
|
+
* Runtime environment provided during the execution of integration's code.
|
|
114
|
+
*/
|
|
115
|
+
export interface IntegrationEnvironment {
|
|
116
|
+
/** URL of the HTTP API */
|
|
117
|
+
apiEndpoint: string;
|
|
118
|
+
/** Authentication token to use with the HTTP API */
|
|
119
|
+
authToken: string;
|
|
120
|
+
integration: Integration;
|
|
121
|
+
installation: IntegrationInstallation;
|
|
122
|
+
/** Secrets stored on the integration and passed at runtime. */
|
|
123
|
+
secrets: IntegrationSecrets;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Any event that can be received from GitBook.
|
|
127
|
+
*/
|
|
128
|
+
export declare type Event = InstallationSetupEvent | SpaceViewEvent | SpaceContentUpdatedEvent | FetchEvent;
|
|
129
|
+
/**
|
|
130
|
+
* Common properties for all events.
|
|
131
|
+
*/
|
|
132
|
+
export interface BaseEvent {
|
|
133
|
+
/** Unique identifier for the event. */
|
|
134
|
+
eventId: string;
|
|
135
|
+
/** Type of the event. */
|
|
136
|
+
type: string;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Common properties for all events related to an installation
|
|
140
|
+
*/
|
|
141
|
+
export declare type InstallationEvent = BaseEvent & {
|
|
142
|
+
installationId: string;
|
|
143
|
+
};
|
|
144
|
+
/**
|
|
145
|
+
* Common properties for all events related to a specific space.
|
|
146
|
+
*/
|
|
147
|
+
export declare type SpaceEvent = InstallationEvent & {
|
|
148
|
+
spaceId: string;
|
|
149
|
+
};
|
|
150
|
+
/**
|
|
151
|
+
* Event received when integration has been installed.
|
|
152
|
+
*/
|
|
153
|
+
export declare type InstallationSetupEvent = SpaceEvent & {
|
|
154
|
+
type: "installation:setup";
|
|
155
|
+
status: IntegrationInstallationStatus;
|
|
156
|
+
};
|
|
157
|
+
/**
|
|
158
|
+
* Event received when a page has been visited.
|
|
159
|
+
*/
|
|
160
|
+
export declare type SpaceViewEvent = SpaceEvent & {
|
|
161
|
+
type: "space:view";
|
|
162
|
+
pageId?: string;
|
|
163
|
+
visitorId: string;
|
|
164
|
+
visitorIp: string;
|
|
165
|
+
visitorUserAgent: string;
|
|
166
|
+
};
|
|
167
|
+
/**
|
|
168
|
+
* Event when the primary content of a space has been updated.
|
|
169
|
+
*/
|
|
170
|
+
export declare type SpaceContentUpdatedEvent = SpaceEvent & {
|
|
171
|
+
type: "space:content:updated";
|
|
172
|
+
revisionId: string;
|
|
173
|
+
};
|
|
174
|
+
export interface FetchRequest {
|
|
175
|
+
method: "post" | "get" | "put" | "delete";
|
|
176
|
+
url: string;
|
|
177
|
+
body?: string;
|
|
178
|
+
headers: Record<string, string>;
|
|
179
|
+
}
|
|
180
|
+
export interface FetchResponse {
|
|
181
|
+
status: number;
|
|
182
|
+
body: string;
|
|
183
|
+
headers: Record<string, string>;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Event representing an incoming HTTP request.
|
|
187
|
+
*/
|
|
188
|
+
export declare type FetchEvent = InstallationEvent & {
|
|
189
|
+
type: "fetch";
|
|
190
|
+
request: FetchRequest;
|
|
191
|
+
};
|
|
192
|
+
export declare type QueryParamsType = Record<string | number, any>;
|
|
193
|
+
export declare type ResponseFormat = keyof Omit<Body, "body" | "bodyUsed">;
|
|
194
|
+
export interface FullRequestParams extends Omit<RequestInit, "body"> {
|
|
195
|
+
/** set parameter to `true` for call `securityWorker` for this request */
|
|
196
|
+
secure?: boolean;
|
|
197
|
+
/** request path */
|
|
198
|
+
path: string;
|
|
199
|
+
/** content type of request body */
|
|
200
|
+
type?: ContentType;
|
|
201
|
+
/** query params */
|
|
202
|
+
query?: QueryParamsType;
|
|
203
|
+
/** format of response (i.e. response.json() -> format: "json") */
|
|
204
|
+
format?: ResponseFormat;
|
|
205
|
+
/** request body */
|
|
206
|
+
body?: unknown;
|
|
207
|
+
/** base url */
|
|
208
|
+
baseUrl?: string;
|
|
209
|
+
/** request cancellation token */
|
|
210
|
+
cancelToken?: CancelToken;
|
|
211
|
+
}
|
|
212
|
+
export declare type RequestParams = Omit<FullRequestParams, "body" | "method" | "query" | "path">;
|
|
213
|
+
export interface ApiConfig<SecurityDataType = unknown> {
|
|
214
|
+
baseUrl?: string;
|
|
215
|
+
baseApiParams?: Omit<RequestParams, "baseUrl" | "cancelToken" | "signal">;
|
|
216
|
+
securityWorker?: (securityData: SecurityDataType | null) => Promise<RequestParams | void> | RequestParams | void;
|
|
217
|
+
customFetch?: typeof fetch;
|
|
218
|
+
}
|
|
219
|
+
export interface HttpResponse<D extends unknown, E extends unknown = unknown> extends Response {
|
|
220
|
+
data: D;
|
|
221
|
+
error: E;
|
|
222
|
+
}
|
|
223
|
+
declare type CancelToken = Symbol | string | number;
|
|
224
|
+
export declare enum ContentType {
|
|
225
|
+
Json = "application/json",
|
|
226
|
+
FormData = "multipart/form-data",
|
|
227
|
+
UrlEncoded = "application/x-www-form-urlencoded"
|
|
228
|
+
}
|
|
229
|
+
export declare class HttpClient<SecurityDataType = unknown> {
|
|
230
|
+
baseUrl: string;
|
|
231
|
+
private securityData;
|
|
232
|
+
private securityWorker?;
|
|
233
|
+
private abortControllers;
|
|
234
|
+
private customFetch;
|
|
235
|
+
private baseApiParams;
|
|
236
|
+
constructor(apiConfig?: ApiConfig<SecurityDataType>);
|
|
237
|
+
setSecurityData: (data: SecurityDataType | null) => void;
|
|
238
|
+
private encodeQueryParam;
|
|
239
|
+
private addQueryParam;
|
|
240
|
+
private addArrayQueryParam;
|
|
241
|
+
protected toQueryString(rawQuery?: QueryParamsType): string;
|
|
242
|
+
protected addQueryParams(rawQuery?: QueryParamsType): string;
|
|
243
|
+
private contentFormatters;
|
|
244
|
+
private mergeRequestParams;
|
|
245
|
+
private createAbortSignal;
|
|
246
|
+
abortRequest: (cancelToken: CancelToken) => void;
|
|
247
|
+
request: <T = any, E = any>({ body, secure, path, type, query, format, baseUrl, cancelToken, ...params }: FullRequestParams) => Promise<HttpResponse<T, E>>;
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* @title GitBook API
|
|
251
|
+
* @version 0.0.1-beta
|
|
252
|
+
* @termsOfService https://policies.gitbook.com
|
|
253
|
+
* @baseUrl {host}/v1
|
|
254
|
+
* @contact API Support <support@gitbook.com> (https://gitbook.com/support)
|
|
255
|
+
*
|
|
256
|
+
* The GitBook API
|
|
257
|
+
*/
|
|
258
|
+
export declare class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDataType> {
|
|
259
|
+
user: {
|
|
260
|
+
/**
|
|
261
|
+
* No description
|
|
262
|
+
*
|
|
263
|
+
* @name GetAuthenticated
|
|
264
|
+
* @summary Get profile of authenticated user
|
|
265
|
+
* @request GET:/user
|
|
266
|
+
* @secure
|
|
267
|
+
*/
|
|
268
|
+
getAuthenticated: (params?: RequestParams) => Promise<HttpResponse<User, Error>>;
|
|
269
|
+
};
|
|
270
|
+
spaces: {
|
|
271
|
+
/**
|
|
272
|
+
* No description
|
|
273
|
+
*
|
|
274
|
+
* @name GetSpace
|
|
275
|
+
* @summary Get the details about a space
|
|
276
|
+
* @request GET:/spaces/{spaceId}
|
|
277
|
+
* @secure
|
|
278
|
+
*/
|
|
279
|
+
getSpace: (spaceId: string, params?: RequestParams) => Promise<HttpResponse<Space, Error>>;
|
|
280
|
+
/**
|
|
281
|
+
* @description Track a page view on a space
|
|
282
|
+
*
|
|
283
|
+
* @name TrackPageView
|
|
284
|
+
* @request POST:/spaces/{spaceId}/views
|
|
285
|
+
* @secure
|
|
286
|
+
*/
|
|
287
|
+
trackPageView: (spaceId: string, data: RequestSpaceTrackPageView, params?: RequestParams) => Promise<HttpResponse<object, Error>>;
|
|
288
|
+
};
|
|
289
|
+
integrations: {
|
|
290
|
+
/**
|
|
291
|
+
* No description
|
|
292
|
+
*
|
|
293
|
+
* @name Publish
|
|
294
|
+
* @summary Publish an integration
|
|
295
|
+
* @request POST:/integrations/{integrationName}
|
|
296
|
+
* @secure
|
|
297
|
+
*/
|
|
298
|
+
publish: (integrationName: string, data: RequestPublishIntegration, params?: RequestParams) => Promise<HttpResponse<Integration, Error>>;
|
|
299
|
+
/**
|
|
300
|
+
* No description
|
|
301
|
+
*
|
|
302
|
+
* @name GetInstallation
|
|
303
|
+
* @summary Get the status and configurations of an integration's installation
|
|
304
|
+
* @request GET:/integrations/{integrationName}/installations/{installationId}
|
|
305
|
+
* @secure
|
|
306
|
+
*/
|
|
307
|
+
getInstallation: (integrationName: string, installationId: string, params?: RequestParams) => Promise<HttpResponse<IntegrationInstallation, Error>>;
|
|
308
|
+
/**
|
|
309
|
+
* No description
|
|
310
|
+
*
|
|
311
|
+
* @name UpdateInstallation
|
|
312
|
+
* @summary Update status and configurations of an integration's installation
|
|
313
|
+
* @request PATCH:/integrations/{integrationName}/installations/{installationId}
|
|
314
|
+
* @secure
|
|
315
|
+
*/
|
|
316
|
+
updateInstallation: (integrationName: string, installationId: string, data: RequestUpdateIntegrationInstallation, params?: RequestParams) => Promise<HttpResponse<object, Error>>;
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
export {};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Api } from './client';
|
|
2
|
+
export * from './client';
|
|
3
|
+
export declare const GITBOOK_DEFAULT_ENDPOINT = "https://api.gitbook.com";
|
|
4
|
+
export declare class GitBookAPI extends Api<{
|
|
5
|
+
authToken?: string;
|
|
6
|
+
}> {
|
|
7
|
+
constructor(options?: {
|
|
8
|
+
/**
|
|
9
|
+
* API endpoint to use.
|
|
10
|
+
* @default "https://api.gitbook.com"
|
|
11
|
+
*/
|
|
12
|
+
endpoint?: string;
|
|
13
|
+
/**
|
|
14
|
+
* Authentication token to use.
|
|
15
|
+
*/
|
|
16
|
+
authToken?: string;
|
|
17
|
+
});
|
|
18
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// src/index.ts
|
|
20
|
+
var src_exports = {};
|
|
21
|
+
__export(src_exports, {
|
|
22
|
+
Api: () => Api,
|
|
23
|
+
ContentType: () => ContentType,
|
|
24
|
+
GITBOOK_DEFAULT_ENDPOINT: () => GITBOOK_DEFAULT_ENDPOINT,
|
|
25
|
+
GitBookAPI: () => GitBookAPI,
|
|
26
|
+
HttpClient: () => HttpClient,
|
|
27
|
+
IntegrationCategory: () => IntegrationCategory,
|
|
28
|
+
IntegrationInstallationStatus: () => IntegrationInstallationStatus,
|
|
29
|
+
IntegrationScope: () => IntegrationScope,
|
|
30
|
+
IntegrationVisibility: () => IntegrationVisibility
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(src_exports);
|
|
33
|
+
|
|
34
|
+
// src/client.ts
|
|
35
|
+
var IntegrationScope = /* @__PURE__ */ ((IntegrationScope2) => {
|
|
36
|
+
IntegrationScope2["SpaceViews"] = "space:views";
|
|
37
|
+
IntegrationScope2["SpaceContent"] = "space:content";
|
|
38
|
+
return IntegrationScope2;
|
|
39
|
+
})(IntegrationScope || {});
|
|
40
|
+
var IntegrationCategory = /* @__PURE__ */ ((IntegrationCategory2) => {
|
|
41
|
+
IntegrationCategory2["Analytics"] = "analytics";
|
|
42
|
+
IntegrationCategory2["Collaboration"] = "collaboration";
|
|
43
|
+
IntegrationCategory2["Other"] = "other";
|
|
44
|
+
return IntegrationCategory2;
|
|
45
|
+
})(IntegrationCategory || {});
|
|
46
|
+
var IntegrationVisibility = /* @__PURE__ */ ((IntegrationVisibility2) => {
|
|
47
|
+
IntegrationVisibility2["Public"] = "public";
|
|
48
|
+
IntegrationVisibility2["Private"] = "private";
|
|
49
|
+
return IntegrationVisibility2;
|
|
50
|
+
})(IntegrationVisibility || {});
|
|
51
|
+
var IntegrationInstallationStatus = /* @__PURE__ */ ((IntegrationInstallationStatus2) => {
|
|
52
|
+
IntegrationInstallationStatus2["Active"] = "active";
|
|
53
|
+
IntegrationInstallationStatus2["Pending"] = "pending";
|
|
54
|
+
IntegrationInstallationStatus2["Paused"] = "paused";
|
|
55
|
+
return IntegrationInstallationStatus2;
|
|
56
|
+
})(IntegrationInstallationStatus || {});
|
|
57
|
+
var ContentType = /* @__PURE__ */ ((ContentType2) => {
|
|
58
|
+
ContentType2["Json"] = "application/json";
|
|
59
|
+
ContentType2["FormData"] = "multipart/form-data";
|
|
60
|
+
ContentType2["UrlEncoded"] = "application/x-www-form-urlencoded";
|
|
61
|
+
return ContentType2;
|
|
62
|
+
})(ContentType || {});
|
|
63
|
+
var HttpClient = class {
|
|
64
|
+
constructor(apiConfig = {}) {
|
|
65
|
+
this.baseUrl = "{host}/v1";
|
|
66
|
+
this.securityData = null;
|
|
67
|
+
this.abortControllers = /* @__PURE__ */ new Map();
|
|
68
|
+
this.customFetch = (...fetchParams) => fetch(...fetchParams);
|
|
69
|
+
this.baseApiParams = {
|
|
70
|
+
credentials: "same-origin",
|
|
71
|
+
headers: {},
|
|
72
|
+
redirect: "follow",
|
|
73
|
+
referrerPolicy: "no-referrer"
|
|
74
|
+
};
|
|
75
|
+
this.setSecurityData = (data) => {
|
|
76
|
+
this.securityData = data;
|
|
77
|
+
};
|
|
78
|
+
this.contentFormatters = {
|
|
79
|
+
["application/json" /* Json */]: (input) => input !== null && (typeof input === "object" || typeof input === "string") ? JSON.stringify(input) : input,
|
|
80
|
+
["multipart/form-data" /* FormData */]: (input) => Object.keys(input || {}).reduce((formData, key) => {
|
|
81
|
+
const property = input[key];
|
|
82
|
+
formData.append(key, property instanceof Blob ? property : typeof property === "object" && property !== null ? JSON.stringify(property) : `${property}`);
|
|
83
|
+
return formData;
|
|
84
|
+
}, new FormData()),
|
|
85
|
+
["application/x-www-form-urlencoded" /* UrlEncoded */]: (input) => this.toQueryString(input)
|
|
86
|
+
};
|
|
87
|
+
this.createAbortSignal = (cancelToken) => {
|
|
88
|
+
if (this.abortControllers.has(cancelToken)) {
|
|
89
|
+
const abortController2 = this.abortControllers.get(cancelToken);
|
|
90
|
+
if (abortController2) {
|
|
91
|
+
return abortController2.signal;
|
|
92
|
+
}
|
|
93
|
+
return void 0;
|
|
94
|
+
}
|
|
95
|
+
const abortController = new AbortController();
|
|
96
|
+
this.abortControllers.set(cancelToken, abortController);
|
|
97
|
+
return abortController.signal;
|
|
98
|
+
};
|
|
99
|
+
this.abortRequest = (cancelToken) => {
|
|
100
|
+
const abortController = this.abortControllers.get(cancelToken);
|
|
101
|
+
if (abortController) {
|
|
102
|
+
abortController.abort();
|
|
103
|
+
this.abortControllers.delete(cancelToken);
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
this.request = async ({
|
|
107
|
+
body,
|
|
108
|
+
secure,
|
|
109
|
+
path,
|
|
110
|
+
type,
|
|
111
|
+
query,
|
|
112
|
+
format,
|
|
113
|
+
baseUrl,
|
|
114
|
+
cancelToken,
|
|
115
|
+
...params
|
|
116
|
+
}) => {
|
|
117
|
+
const secureParams = (typeof secure === "boolean" ? secure : this.baseApiParams.secure) && this.securityWorker && await this.securityWorker(this.securityData) || {};
|
|
118
|
+
const requestParams = this.mergeRequestParams(params, secureParams);
|
|
119
|
+
const queryString = query && this.toQueryString(query);
|
|
120
|
+
const payloadFormatter = this.contentFormatters[type || "application/json" /* Json */];
|
|
121
|
+
const responseFormat = format || requestParams.format;
|
|
122
|
+
return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, {
|
|
123
|
+
...requestParams,
|
|
124
|
+
headers: {
|
|
125
|
+
...type && type !== "multipart/form-data" /* FormData */ ? { "Content-Type": type } : {},
|
|
126
|
+
...requestParams.headers || {}
|
|
127
|
+
},
|
|
128
|
+
signal: cancelToken ? this.createAbortSignal(cancelToken) : void 0,
|
|
129
|
+
body: typeof body === "undefined" || body === null ? null : payloadFormatter(body)
|
|
130
|
+
}).then(async (response) => {
|
|
131
|
+
const r = response;
|
|
132
|
+
r.data = null;
|
|
133
|
+
r.error = null;
|
|
134
|
+
const data = !responseFormat ? r : await response[responseFormat]().then((data2) => {
|
|
135
|
+
if (r.ok) {
|
|
136
|
+
r.data = data2;
|
|
137
|
+
} else {
|
|
138
|
+
r.error = data2;
|
|
139
|
+
}
|
|
140
|
+
return r;
|
|
141
|
+
}).catch((e) => {
|
|
142
|
+
r.error = e;
|
|
143
|
+
return r;
|
|
144
|
+
});
|
|
145
|
+
if (cancelToken) {
|
|
146
|
+
this.abortControllers.delete(cancelToken);
|
|
147
|
+
}
|
|
148
|
+
if (!response.ok)
|
|
149
|
+
throw data;
|
|
150
|
+
return data;
|
|
151
|
+
});
|
|
152
|
+
};
|
|
153
|
+
Object.assign(this, apiConfig);
|
|
154
|
+
}
|
|
155
|
+
encodeQueryParam(key, value) {
|
|
156
|
+
const encodedKey = encodeURIComponent(key);
|
|
157
|
+
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
|
|
158
|
+
}
|
|
159
|
+
addQueryParam(query, key) {
|
|
160
|
+
return this.encodeQueryParam(key, query[key]);
|
|
161
|
+
}
|
|
162
|
+
addArrayQueryParam(query, key) {
|
|
163
|
+
const value = query[key];
|
|
164
|
+
return value.map((v) => this.encodeQueryParam(key, v)).join("&");
|
|
165
|
+
}
|
|
166
|
+
toQueryString(rawQuery) {
|
|
167
|
+
const query = rawQuery || {};
|
|
168
|
+
const keys = Object.keys(query).filter((key) => typeof query[key] !== "undefined");
|
|
169
|
+
return keys.map((key) => Array.isArray(query[key]) ? this.addArrayQueryParam(query, key) : this.addQueryParam(query, key)).join("&");
|
|
170
|
+
}
|
|
171
|
+
addQueryParams(rawQuery) {
|
|
172
|
+
const queryString = this.toQueryString(rawQuery);
|
|
173
|
+
return queryString ? `?${queryString}` : "";
|
|
174
|
+
}
|
|
175
|
+
mergeRequestParams(params1, params2) {
|
|
176
|
+
return {
|
|
177
|
+
...this.baseApiParams,
|
|
178
|
+
...params1,
|
|
179
|
+
...params2 || {},
|
|
180
|
+
headers: {
|
|
181
|
+
...this.baseApiParams.headers || {},
|
|
182
|
+
...params1.headers || {},
|
|
183
|
+
...params2 && params2.headers || {}
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
var Api = class extends HttpClient {
|
|
189
|
+
constructor() {
|
|
190
|
+
super(...arguments);
|
|
191
|
+
this.user = {
|
|
192
|
+
getAuthenticated: (params = {}) => this.request({
|
|
193
|
+
path: `/user`,
|
|
194
|
+
method: "GET",
|
|
195
|
+
secure: true,
|
|
196
|
+
format: "json",
|
|
197
|
+
...params
|
|
198
|
+
})
|
|
199
|
+
};
|
|
200
|
+
this.spaces = {
|
|
201
|
+
getSpace: (spaceId, params = {}) => this.request({
|
|
202
|
+
path: `/spaces/${spaceId}`,
|
|
203
|
+
method: "GET",
|
|
204
|
+
secure: true,
|
|
205
|
+
format: "json",
|
|
206
|
+
...params
|
|
207
|
+
}),
|
|
208
|
+
trackPageView: (spaceId, data, params = {}) => this.request({
|
|
209
|
+
path: `/spaces/${spaceId}/views`,
|
|
210
|
+
method: "POST",
|
|
211
|
+
body: data,
|
|
212
|
+
secure: true,
|
|
213
|
+
type: "application/json" /* Json */,
|
|
214
|
+
...params
|
|
215
|
+
})
|
|
216
|
+
};
|
|
217
|
+
this.integrations = {
|
|
218
|
+
publish: (integrationName, data, params = {}) => this.request({
|
|
219
|
+
path: `/integrations/${integrationName}`,
|
|
220
|
+
method: "POST",
|
|
221
|
+
body: data,
|
|
222
|
+
secure: true,
|
|
223
|
+
type: "application/json" /* Json */,
|
|
224
|
+
format: "json",
|
|
225
|
+
...params
|
|
226
|
+
}),
|
|
227
|
+
getInstallation: (integrationName, installationId, params = {}) => this.request({
|
|
228
|
+
path: `/integrations/${integrationName}/installations/${installationId}`,
|
|
229
|
+
method: "GET",
|
|
230
|
+
secure: true,
|
|
231
|
+
format: "json",
|
|
232
|
+
...params
|
|
233
|
+
}),
|
|
234
|
+
updateInstallation: (integrationName, installationId, data, params = {}) => this.request({
|
|
235
|
+
path: `/integrations/${integrationName}/installations/${installationId}`,
|
|
236
|
+
method: "PATCH",
|
|
237
|
+
body: data,
|
|
238
|
+
secure: true,
|
|
239
|
+
type: "application/json" /* Json */,
|
|
240
|
+
...params
|
|
241
|
+
})
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
// src/index.ts
|
|
247
|
+
var GITBOOK_DEFAULT_ENDPOINT = "https://api.gitbook.com";
|
|
248
|
+
var GitBookAPIError = class extends Error {
|
|
249
|
+
constructor(response) {
|
|
250
|
+
super(response.error?.error.message || "Unknown error");
|
|
251
|
+
this.response = response;
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
var GitBookAPI = class extends Api {
|
|
255
|
+
constructor(options = {}) {
|
|
256
|
+
const { endpoint = GITBOOK_DEFAULT_ENDPOINT, authToken } = options;
|
|
257
|
+
super({
|
|
258
|
+
baseUrl: `${endpoint}/v1`,
|
|
259
|
+
securityWorker: (securityData) => {
|
|
260
|
+
if (securityData.authToken) {
|
|
261
|
+
return {
|
|
262
|
+
headers: {
|
|
263
|
+
Authorization: `Bearer ${securityData.authToken}`
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
return {};
|
|
268
|
+
}
|
|
269
|
+
});
|
|
270
|
+
const request = this.request;
|
|
271
|
+
this.request = async (...args) => {
|
|
272
|
+
try {
|
|
273
|
+
const response = await request(...args);
|
|
274
|
+
return response;
|
|
275
|
+
} catch (error) {
|
|
276
|
+
if (error instanceof Error) {
|
|
277
|
+
throw error;
|
|
278
|
+
}
|
|
279
|
+
throw new GitBookAPIError(error);
|
|
280
|
+
}
|
|
281
|
+
};
|
|
282
|
+
this.setSecurityData({ authToken });
|
|
283
|
+
}
|
|
284
|
+
};
|
|
285
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
286
|
+
0 && (module.exports = {
|
|
287
|
+
Api,
|
|
288
|
+
ContentType,
|
|
289
|
+
GITBOOK_DEFAULT_ENDPOINT,
|
|
290
|
+
GitBookAPI,
|
|
291
|
+
HttpClient,
|
|
292
|
+
IntegrationCategory,
|
|
293
|
+
IntegrationInstallationStatus,
|
|
294
|
+
IntegrationScope,
|
|
295
|
+
IntegrationVisibility
|
|
296
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@gitbook/api",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"dependencies": {},
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"spec/**",
|
|
9
|
+
"dist/**"
|
|
10
|
+
],
|
|
11
|
+
"devDependencies": {
|
|
12
|
+
"swagger-typescript-api": "^9.3.1",
|
|
13
|
+
"swagger-cli": "^4.0.4",
|
|
14
|
+
"esbuild": "^0.14.42",
|
|
15
|
+
"typescript": "^4.6.3",
|
|
16
|
+
"eslint": "^8.16.0",
|
|
17
|
+
"@gitbook/eslint-config": "*",
|
|
18
|
+
"@gitbook/tsconfig": "*"
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "./build.sh",
|
|
22
|
+
"lint": "eslint ./src/**/*.ts",
|
|
23
|
+
"typecheck": "tsc --noEmit"
|
|
24
|
+
}
|
|
25
|
+
}
|