-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgotenberg.ts
54 lines (49 loc) · 1.48 KB
/
gotenberg.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
import { URL } from 'url' // tslint:disable-line no-circular-imports
import { client as native } from './client/node'
import {
GotenbergClient,
GotenbergClientClass,
GotenbergClientFunction,
Request,
RequestType,
Source,
} from './_types'
/**
* Initializes Gotenberg request
*/
export const gotenberg = (
url: string | Buffer | URL,
client?: GotenbergClient | GotenbergClientFunction | GotenbergClientClass | object,
config?: object
) => {
let instance: GotenbergClient
// if GotenbergClient object / instance provided -> just use it
if (typeof client === 'object' && 'post' in client) {
instance = client
}
// if GotenbergClientFunction or GotenbergClientClass -> call or instantiate it
else if (typeof client === 'function') {
// there is no good way to distinguish regular function from class,
// hope this will do ¯\_(ツ)_/¯
if (/^class\s/.test(Function.prototype.toString.call(client))) {
// guess this is GotenbergClientClass
instance = new (client as GotenbergClientClass)(config)
} else {
// guess this is GotenbergClientFunction
instance = (client as GotenbergClientFunction)(config)
}
}
// there is config object instead of client given
// (or maybe it is just undefined)
// -> use native client
else {
instance = native(config || client)
}
return (source: Source): Request => ({
type: RequestType.Undefined,
url: url.toString(),
client: instance,
source,
fields: {},
})
}