-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path_types.ts
212 lines (181 loc) · 7.37 KB
/
_types.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
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
import { URL } from 'url' // tslint:disable-line no-circular-imports
import FormData from 'form-data'
////////////////////////////////////////////////////////////////////////////////
/// gotenberg client ///////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/**
* Gotenberg client interface
*/
export interface GotenbergClient {
post: (
url: string,
data: FormData,
headers?: {
[header: string]: number | string | string[] | undefined
}
) => Promise<NodeJS.ReadableStream>
get?: (url: string) => Promise<NodeJS.ReadableStream>
}
/**
* Gotenberg client interface with configurator function
* Will be called with config third gotenberg object
*/
export interface GotenbergClientFunction {
(config?: object): GotenbergClient
}
/**
* Gotenberg client class interface
* Will be initialized with `new (config)` third gotenberg object
*/
export interface GotenbergClientClass {
new (config?: object): GotenbergClient
}
////////////////////////////////////////////////////////////////////////////////
/// form fields ////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// common form fields, for any conversion
export type CommonRequestFields = {
// It takes a float as value (e.g 2.5 for 2.5 seconds)
// https://thecodingmachine.github.io/gotenberg/#timeout
waitTimeout?: number
// If provided, the API will send the resulting PDF file in a POST request with the `application/pdf` Content-Type to given URL
// https://thecodingmachine.github.io/gotenberg/#webhook
webhookURL?: string
// It takes a float as value (e.g 2.5 for 2.5 seconds)
// https://thecodingmachine.github.io/gotenberg/#webhook.timeout
webhookURLTimeout?: number
// If provided, the API will return the resulting PDF file with the given filename. Otherwise a random filename is used
// Attention: this feature does not work if the form field webhookURL is given
// https://thecodingmachine.github.io/gotenberg/#result_filename
resultFilename?: string
}
// chrome form fields
export type ChromeRequestFields = {
// The wait delay is a duration in seconds (e.g 2.5 for 2.5 seconds)
// https://thecodingmachine.github.io/gotenberg/#html.wait_delay
waitDelay?: number
// It takes an integer as value (e.g. 1048576 for 1 MB). The hard limit is 100 MB and is defined by Google Chrome itself
// https://thecodingmachine.github.io/gotenberg/#html.rpcc_buffer_size
googleChromeRpccBufferSize?: number
}
// html conversion form fields
// https://thecodingmachine.github.io/gotenberg/#html
export type HtmlRequestFields = {
// By default, it will be rendered with A4 size, 1 inch margins and portrait orientation
// Paper size and margins have to be provided in inches. Same for margins
// https://thecodingmachine.github.io/gotenberg/#html.paper_size_margins_orientation
paperWidth?: number
paperHeight?: number
marginTop?: number
marginBottom?: number
marginLeft?: number
marginRight?: number
landscape?: boolean
// https://thecodingmachine.github.io/gotenberg/#html.page_ranges
pageRanges?: string
// https://thecodingmachine.github.io/gotenberg/#html.paper_size_margins_orientation_scaling
scale?: number
}
// markdown conversion form fields
// Markdown conversions work the same as HTML conversions
// https://thecodingmachine.github.io/gotenberg/#markdown
export type MarkdownRequestFields = HtmlRequestFields
// office documents conversion form fields
// https://thecodingmachine.github.io/gotenberg/#office
export type OfficeRequestFields = {
// By default, it will be rendered with portrait orientation
// https://thecodingmachine.github.io/gotenberg/#office.orientation
landscape?: boolean
// https://thecodingmachine.github.io/gotenberg/#office.page_ranges
pageRanges?: string
}
// url conversion form fields
// Attention: when converting a website to PDF, you should remove all margins
// If not, some of the content of the page might be hidden
// https://thecodingmachine.github.io/gotenberg/#url
export type UrlRequestFields = HtmlRequestFields & {
remoteURL?: string
}
// merge conversion doesn't have any form fields
// https://thecodingmachine.github.io/gotenberg/#merge
export type MergeRequestFields = {}
// all available form fields
export type RequestFields = CommonRequestFields &
UrlRequestFields &
HtmlRequestFields &
MergeRequestFields &
OfficeRequestFields &
ChromeRequestFields &
MarkdownRequestFields
////////////////////////////////////////////////////////////////////////////////
/// Html | Markdown | Office extended options (margins | page size) ////////////
////////////////////////////////////////////////////////////////////////////////
export type FieldsModifier = (fields: RequestFields) => void
export type PaperOptions = [number, number] | { width?: number; height?: number }
export type MarginOptions =
| [number, number, number, number]
| { top?: number; right?: number; bottom?: number; left?: number }
export type ConversionOptions =
| PaperOptions
| MarginOptions
| FieldsModifier
| (HtmlRequestFields &
OfficeRequestFields &
MarkdownRequestFields & { paper?: PaperOptions } & {
margins?: MarginOptions
})
////////////////////////////////////////////////////////////////////////////////
/// available sources //////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
export type FileURI = string // TODO: https://github.com/microsoft/TypeScript/issues/6579
export type PlainSource = string | Buffer | FileURI | NodeJS.ReadableStream
export type TupleSource = [string, PlainSource]
export type ObjectSource = { [name: string]: PlainSource }
export type Source =
| URL // for url conversions
| PlainSource
| TupleSource
| ObjectSource
| Array<PlainSource | TupleSource | ObjectSource>
| Iterable<PlainSource | TupleSource | ObjectSource>
export type TupleStreamsSource = [string, NodeJS.ReadableStream]
////////////////////////////////////////////////////////////////////////////////
/// request headers ////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
export type HeadersModifier = (headers: HttpHeaders) => void
export type HttpHeaders = {
[header: string]: number | string
}
////////////////////////////////////////////////////////////////////////////////
/// request types //////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
export enum RequestType {
Url,
Ping,
Html,
Merge,
Office,
Markdown,
Undefined,
}
export type Request = {
type: RequestType
url: string
client: GotenbergClient
source?: Source
fields: RequestFields
headers?: HttpHeaders
}
export type UrlRequest = Request & { type: RequestType.Url }
export type PingRequest = Request & { type: RequestType.Ping }
export type HtmlRequest = Request & { type: RequestType.Html }
export type MergeRequest = Request & { type: RequestType.Merge }
export type OfficeRequest = Request & { type: RequestType.Office }
export type MarkdownRequest = Request & { type: RequestType.Markdown }
export type TypedRequest =
| UrlRequest
| PingRequest
| HtmlRequest
| MergeRequest
| OfficeRequest
| MarkdownRequest