Skip to content

Commit 93f86bc

Browse files
committed
create 2 entry points
- one for sideloading into UI (src/sideload.ts) - another to be used as a package (src/index.ts)
1 parent 5f322d8 commit 93f86bc

File tree

5 files changed

+71
-32
lines changed

5 files changed

+71
-32
lines changed

examples/basic.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import Docupilot from '../src';
1+
import { client } from '../src';
22

33
async function run() {
4-
const Client = await Docupilot.authorize();
5-
const user = await Client.UsersService.getMe();
6-
console.log(`Docupilot client authenticated as ${user.email}`);
7-
console.log(await Client.FoldersService.listFolders({}));
8-
console.log(await Client.TemplatesService.listTemplates({}));
4+
await client.authenticate();
5+
if (await client.isAuthenticated()) {
6+
console.log(await client.FoldersService.listFolders({}));
7+
console.log(await client.TemplatesService.listTemplates({}));
8+
}
99
}
1010

1111
run()

src/docupilot.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import * as _API from './api';
2+
import { ApiError } from './api';
3+
4+
export default class Docupilot {
5+
readonly UsersService = _API.UsersService;
6+
7+
async isAuthenticated(): Promise<boolean> {
8+
try {
9+
await this.UsersService.getMe();
10+
return true;
11+
} catch (e) {
12+
if ((e as ApiError).status == 403) {
13+
console.log('invalid credentials');
14+
return false;
15+
}
16+
console.log((e as ApiError).statusText);
17+
return false;
18+
}
19+
}
20+
21+
async authenticate(
22+
access_key: string = process.env.DOCUPILOT_ACCESS_KEY as string,
23+
access_secret: string = process.env.DOCUPILOT_ACCESS_SECRET as string,
24+
host: string = process.env.DOCUPILOT_HOST as string,
25+
) {
26+
_API.OpenAPI.BASE = (host ?? 'https://api.docupilot.app').replace(
27+
/\/+$/g,
28+
'',
29+
);
30+
_API.OpenAPI.TOKEN = btoa(`${access_key}:${access_secret}`);
31+
}
32+
}

src/index.ts

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,15 @@
1-
import * as API from './api';
1+
import Docupilot from './docupilot';
2+
import * as _API from './api';
23

3-
class Client {
4-
static readonly FoldersService = API.FoldersService;
5-
static readonly TemplatesService = API.TemplatesService;
6-
static readonly GenerateService = API.GenerateService;
7-
static readonly HistoryService = API.HistoryService;
8-
static readonly LinkedAccountsService = API.LinkedAccountsService;
9-
static readonly GenerateBulkService = API.GenerateBulkService;
10-
static readonly TemplateDeliveryService = API.TemplateDeliveryService;
11-
static readonly UsersService = API.UsersService;
4+
class _Docupilot extends Docupilot {
5+
readonly FoldersService = _API.FoldersService;
6+
readonly TemplatesService = _API.TemplatesService;
7+
readonly GenerateService = _API.GenerateService;
8+
readonly HistoryService = _API.HistoryService;
9+
readonly LinkedAccountsService = _API.LinkedAccountsService;
10+
readonly GenerateBulkService = _API.GenerateBulkService;
11+
readonly TemplateDeliveryService = _API.TemplateDeliveryService;
12+
readonly UsersService = _API.UsersService;
1213
}
1314

14-
export default class Docupilot {
15-
static async authorize(
16-
access_key: string = process.env.DOCUPILOT_ACCESS_KEY as string,
17-
access_secret: string = process.env.DOCUPILOT_ACCESS_SECRET as string,
18-
host: string = process.env.DOCUPILOT_HOST as string,
19-
logger = console.log,
20-
) {
21-
API.OpenAPI.BASE = (host ?? 'https://api.docupilot.app').replace(
22-
/\/+$/g,
23-
'',
24-
);
25-
API.OpenAPI.TOKEN = btoa(`${access_key}:${access_secret}`);
26-
return Client;
27-
}
28-
}
15+
export const client = new _Docupilot();

src/sideload.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import Docupilot from './docupilot';
2+
3+
import * as _API from './api';
4+
5+
class _Docupilot extends Docupilot {
6+
readonly AuthTokensService = _API.AuthTokensService;
7+
readonly FoldersService = _API.FoldersService;
8+
readonly GeneralService = _API.GeneralService;
9+
readonly GenerateBulkService = _API.GenerateBulkService;
10+
readonly GenerateService = _API.GenerateService;
11+
readonly HistoryService = _API.HistoryService;
12+
readonly LinkedAccountsService = _API.LinkedAccountsService;
13+
readonly PersonalizationService = _API.PersonalizationService;
14+
readonly SubscriptionService = _API.SubscriptionService;
15+
readonly TemplateDeliveryService = _API.TemplateDeliveryService;
16+
readonly TemplatesService = _API.TemplatesService;
17+
readonly UsersService = _API.UsersService;
18+
}
19+
20+
export const client = new _Docupilot();

webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const path = require('path');
22

33
const config = {
4-
entry: './src/index.ts',
4+
entry: './src/sideload.ts',
55
target: ['web', 'es5'],
66
module: {
77
rules: [

0 commit comments

Comments
 (0)