Some size and clarity optimizations
This commit is contained in:
+1
-1
@@ -25,7 +25,7 @@
|
||||
{
|
||||
"path": "pkg/dist-node/index.js",
|
||||
"webpack": false,
|
||||
"limit": "3938 B"
|
||||
"limit": "3874 B"
|
||||
}
|
||||
],
|
||||
"@pika/pack": {
|
||||
|
||||
+9
-14
@@ -1,22 +1,21 @@
|
||||
import { HeadersModifier, HttpHeaders } from './_types'
|
||||
import { setProperty } from './tools/fn'
|
||||
|
||||
// https://thecodingmachine.github.io/gotenberg/#url.custom_http_headers
|
||||
|
||||
/**
|
||||
* Adds/Modifies single header for Url conversion
|
||||
*/
|
||||
export const header = (name: string, value: number | string): HeadersModifier => (
|
||||
httpHeaders: HttpHeaders
|
||||
) => (httpHeaders[`Gotenberg-Remoteurl-${name}`] = value)
|
||||
export const header = (name: string, value: number | string): HeadersModifier =>
|
||||
setProperty(`Gotenberg-Remoteurl-${name}`)(value)
|
||||
|
||||
/**
|
||||
* Adds/Modifies many headers for Url conversion
|
||||
*/
|
||||
export const headers = (headers: HttpHeaders): HeadersModifier => (httpHeaders: HttpHeaders) => {
|
||||
export const headers = (headers: HttpHeaders): HeadersModifier => _ => {
|
||||
for (const name in headers) {
|
||||
header(name, headers[name])(httpHeaders)
|
||||
header(name, headers[name])(_)
|
||||
}
|
||||
return httpHeaders
|
||||
}
|
||||
|
||||
// https://thecodingmachine.github.io/gotenberg/#webhook.custom_http_headers
|
||||
@@ -24,18 +23,14 @@ export const headers = (headers: HttpHeaders): HeadersModifier => (httpHeaders:
|
||||
/**
|
||||
* Adds/Modifies single header for Webhook
|
||||
*/
|
||||
export const webhookHeader = (name: string, value: number | string): HeadersModifier => (
|
||||
httpHeaders: HttpHeaders
|
||||
) => (httpHeaders[`Gotenberg-Webhookurl-${name}`] = value)
|
||||
export const webhookHeader = (name: string, value: number | string): HeadersModifier =>
|
||||
setProperty(`Gotenberg-Webhookurl-${name}`)(value)
|
||||
|
||||
/**
|
||||
* Adds/Modifies many headers for Webhook
|
||||
*/
|
||||
export const webhookHeaders = (headers: HttpHeaders): HeadersModifier => (
|
||||
httpHeaders: HttpHeaders
|
||||
) => {
|
||||
export const webhookHeaders = (headers: HttpHeaders): HeadersModifier => _ => {
|
||||
for (const name in headers) {
|
||||
webhookHeader(name, headers[name])(httpHeaders)
|
||||
webhookHeader(name, headers[name])(_)
|
||||
}
|
||||
return httpHeaders
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ export const add: {
|
||||
} = (...opts) => {
|
||||
const httpHeaders: HttpHeaders = {}
|
||||
|
||||
// check every given option
|
||||
for (let i = opts.length; i--; ) {
|
||||
const op = opts[i]
|
||||
op(httpHeaders)
|
||||
|
||||
+23
-20
@@ -1,10 +1,12 @@
|
||||
import { FieldsModifier, RequestFields } from './_types'
|
||||
import { FieldsModifier } from './_types'
|
||||
import { setProperty } from './tools/fn'
|
||||
|
||||
/**
|
||||
* Modifies `resultFilename` form field
|
||||
*/
|
||||
export const filename = (name: string): FieldsModifier => (fields: RequestFields) =>
|
||||
(fields.resultFilename = name)
|
||||
export const filename: {
|
||||
(name: string): FieldsModifier
|
||||
} = setProperty('resultFilename')
|
||||
|
||||
/**
|
||||
* Modifies `waitTimeout` form field
|
||||
@@ -13,8 +15,9 @@ export const filename = (name: string): FieldsModifier => (fields: RequestFields
|
||||
* You may increase or decrease this limit thanks to the environment variable `MAXIMUM_WAIT_TIMEOUT`.
|
||||
* https://thecodingmachine.github.io/gotenberg/#environment_variables.maximum_wait_timeout
|
||||
*/
|
||||
export const timeout = (timeout: number): FieldsModifier => (fields: RequestFields) =>
|
||||
(fields.waitTimeout = timeout)
|
||||
export const timeout: {
|
||||
(timeout: number): FieldsModifier
|
||||
} = setProperty('waitTimeout')
|
||||
|
||||
/**
|
||||
* Modifies `waitDelay` form field
|
||||
@@ -23,8 +26,9 @@ export const timeout = (timeout: number): FieldsModifier => (fields: RequestFiel
|
||||
* You may increase or decrease this limit thanks to the environment variable `MAXIMUM_WAIT_DELAY`.
|
||||
* https://thecodingmachine.github.io/gotenberg/#environment_variables.maximum_wait_delay
|
||||
*/
|
||||
export const delay = (delay: number): FieldsModifier => (fields: RequestFields) =>
|
||||
(fields.waitDelay = delay)
|
||||
export const delay: {
|
||||
(delay: number): FieldsModifier
|
||||
} = setProperty('waitDelay')
|
||||
|
||||
/**
|
||||
* Modifies `webhookURL` and `webhookURLTimeout` form fields
|
||||
@@ -33,12 +37,9 @@ export const delay = (delay: number): FieldsModifier => (fields: RequestFields)
|
||||
* You may increase or decrease this limit thanks to the environment variable `MAXIMUM_WEBHOOK_URL_TIMEOUT`.
|
||||
* https://thecodingmachine.github.io/gotenberg/#environment_variables.maximum_webhook_url_timeout
|
||||
*/
|
||||
export const webhook = (url: string, timeout?: number): FieldsModifier => (
|
||||
fields: RequestFields
|
||||
) => {
|
||||
fields.webhookURL = url
|
||||
fields.webhookURLTimeout = timeout
|
||||
}
|
||||
export const webhook: {
|
||||
(url: string, timeout?: number): FieldsModifier
|
||||
} = setProperty('webhookURL', 'webhookURLTimeout')
|
||||
|
||||
/**
|
||||
* Modifies `googleChromeRpccBufferSize` form field
|
||||
@@ -47,9 +48,9 @@ export const webhook = (url: string, timeout?: number): FieldsModifier => (
|
||||
* The hard limit is 100 MB (= 1_048_576_000 B) and is defined by Google Chrome itself.
|
||||
* https://thecodingmachine.github.io/gotenberg/#environment_variables.default_google_chrome_rpcc_buffer_size
|
||||
*/
|
||||
export const googleChromeRpccBufferSize = (googleChromeRpccBufferSize: number): FieldsModifier => (
|
||||
fields: RequestFields
|
||||
) => (fields.googleChromeRpccBufferSize = googleChromeRpccBufferSize)
|
||||
export const googleChromeRpccBufferSize: {
|
||||
(googleChromeRpccBufferSize: number): FieldsModifier
|
||||
} = setProperty('googleChromeRpccBufferSize')
|
||||
|
||||
/**
|
||||
* Modifies `pageRanges` form field
|
||||
@@ -57,13 +58,15 @@ export const googleChromeRpccBufferSize = (googleChromeRpccBufferSize: number):
|
||||
* https://thecodingmachine.github.io/gotenberg/#html.page_ranges
|
||||
* https://thecodingmachine.github.io/gotenberg/#office.page_ranges
|
||||
*/
|
||||
export const range = (range: string): FieldsModifier => (fields: RequestFields) =>
|
||||
(fields.pageRanges = range)
|
||||
export const range: {
|
||||
(range: string): FieldsModifier
|
||||
} = setProperty('pageRanges')
|
||||
|
||||
/**
|
||||
* Modifies `scale` form field
|
||||
*
|
||||
* https://thecodingmachine.github.io/gotenberg/#html.paper_size_margins_orientation_scaling
|
||||
*/
|
||||
export const scale = (scale: number): FieldsModifier => (fields: RequestFields) =>
|
||||
(fields.scale = scale)
|
||||
export const scale: {
|
||||
(scale: number): FieldsModifier
|
||||
} = setProperty('scale')
|
||||
|
||||
@@ -12,7 +12,6 @@ export const set: {
|
||||
} = (...opts) => {
|
||||
const options: RequestFields = {}
|
||||
|
||||
// check every given option
|
||||
for (let i = opts.length; i--; ) {
|
||||
const op = opts[i]
|
||||
if (typeof op === 'function') {
|
||||
|
||||
+17
-33
@@ -1,4 +1,5 @@
|
||||
import { FieldsModifier, MarginOptions, PaperOptions, RequestFields } from './_types'
|
||||
import { FieldsModifier, MarginOptions, PaperOptions } from './_types'
|
||||
import { setProperty } from './tools/fn'
|
||||
import {
|
||||
A3,
|
||||
A4,
|
||||
@@ -12,34 +13,23 @@ import {
|
||||
TABLOID,
|
||||
} from './page'
|
||||
|
||||
/**
|
||||
* Empty form field modifier, do nothing
|
||||
*/
|
||||
const noop: FieldsModifier = () => undefined
|
||||
|
||||
/**
|
||||
* Modifies `landscape` form field to be true
|
||||
*/
|
||||
export const landscape: FieldsModifier = (fields: RequestFields) => (fields.landscape = true)
|
||||
export const landscape: FieldsModifier = setProperty('landscape')(true)
|
||||
|
||||
/**
|
||||
* Modifies `landscape` form field to be undefined (~ false)
|
||||
*/
|
||||
export const portrait: FieldsModifier = (fields: RequestFields) => (fields.landscape = undefined) // == portrait is default orientation
|
||||
export const portrait: FieldsModifier = setProperty('landscape')() // == portrait is default orientation
|
||||
|
||||
/**
|
||||
* Modifies paper size
|
||||
*/
|
||||
export const paperSize = (paper?: PaperOptions): FieldsModifier => {
|
||||
if (!paper) return noop
|
||||
return (fields: RequestFields) => {
|
||||
if (Array.isArray(paper)) {
|
||||
;[fields.paperWidth, fields.paperHeight] = paper
|
||||
} else {
|
||||
;({ width: fields.paperWidth, height: fields.paperHeight } = paper)
|
||||
}
|
||||
}
|
||||
}
|
||||
export const paperSize = (paper: PaperOptions): FieldsModifier =>
|
||||
Array.isArray(paper)
|
||||
? setProperty('paperWidth', 'paperHeight')(...paper)
|
||||
: setProperty('paperWidth', 'paperHeight')(paper.width, paper.height)
|
||||
|
||||
// some predefined paper size modifiers
|
||||
export const a3 = paperSize(A3)
|
||||
@@ -53,21 +43,15 @@ export const tabloid = paperSize(TABLOID)
|
||||
/**
|
||||
* Modifies margins
|
||||
*/
|
||||
export const marginSizes = (margins?: MarginOptions): FieldsModifier => {
|
||||
if (!margins) return noop
|
||||
return (fields: RequestFields) => {
|
||||
if (Array.isArray(margins)) {
|
||||
;[fields.marginTop, fields.marginRight, fields.marginBottom, fields.marginLeft] = margins
|
||||
} else {
|
||||
;({
|
||||
top: fields.marginTop,
|
||||
right: fields.marginRight,
|
||||
bottom: fields.marginBottom,
|
||||
left: fields.marginLeft,
|
||||
} = margins)
|
||||
}
|
||||
}
|
||||
}
|
||||
export const marginSizes = (margins: MarginOptions): FieldsModifier =>
|
||||
Array.isArray(margins)
|
||||
? setProperty('marginTop', 'marginRight', 'marginBottom', 'marginLeft')(...margins)
|
||||
: setProperty('marginTop', 'marginRight', 'marginBottom', 'marginLeft')(
|
||||
margins.top,
|
||||
margins.right,
|
||||
margins.bottom,
|
||||
margins.left
|
||||
)
|
||||
|
||||
// some predefined margins modifiers
|
||||
export const noMargins = marginSizes(NO_MARGINS)
|
||||
|
||||
@@ -71,8 +71,8 @@ export const to: {
|
||||
}
|
||||
|
||||
// update page size and margins, if we have some
|
||||
paperSize(paper)(options)
|
||||
marginSizes(margins)(options)
|
||||
paper && paperSize(paper)(options)
|
||||
margins && marginSizes(margins)(options)
|
||||
|
||||
return fields(options)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
/**
|
||||
* Some short useful functions
|
||||
*/
|
||||
|
||||
export const setProperty = (...fields: string[]) => (...values: any[]) => (object: any) => {
|
||||
for (let i = 0; i < fields.length; i++) {
|
||||
object[fields[i]] = values[i]
|
||||
}
|
||||
}
|
||||
+2
-5
@@ -12,16 +12,13 @@ import { type } from './internal/type'
|
||||
export const url: {
|
||||
(request: Request): UrlRequest
|
||||
} = request => {
|
||||
let remoteURL: string
|
||||
if (isString(request.source)) remoteURL = request.source
|
||||
else if (isURL(request.source)) remoteURL = request.source.toString()
|
||||
else {
|
||||
if (!isString(request.source) && !isURL(request.source)) {
|
||||
throw new Error('Invalid source, should be url string or instance of URL')
|
||||
}
|
||||
|
||||
return pipe(
|
||||
fields({
|
||||
remoteURL,
|
||||
remoteURL: request.source.toString(),
|
||||
|
||||
// set all margins to 0
|
||||
// > Attention: when converting a website to PDF, you should remove all margins.
|
||||
|
||||
+2
-10
@@ -80,7 +80,7 @@ test('Test `tabloid` function', () => {
|
||||
})
|
||||
|
||||
test('Test `paperSize` function', () => {
|
||||
let object = {}
|
||||
const object = {}
|
||||
paperSize([1, 2])(object)
|
||||
expect(object).toEqual({ paperWidth: 1, paperHeight: 2 })
|
||||
paperSize({ width: 5, height: 6 })(object)
|
||||
@@ -89,10 +89,6 @@ test('Test `paperSize` function', () => {
|
||||
expect(object).toEqual({ paperWidth: 10 })
|
||||
paperSize({ height: 15 })(object)
|
||||
expect(object).toEqual({ paperHeight: 15 })
|
||||
|
||||
object = {}
|
||||
paperSize()(object)
|
||||
expect(object).toEqual(object)
|
||||
})
|
||||
|
||||
test('Test `noMargins` function', () => {
|
||||
@@ -129,7 +125,7 @@ test('Test `largeMargins` function', () => {
|
||||
})
|
||||
|
||||
test('Test `marginSizes` function', () => {
|
||||
let object = {}
|
||||
const object = {}
|
||||
marginSizes([1, 2, 3, 4])(object)
|
||||
expect(object).toEqual({
|
||||
marginTop: 1,
|
||||
@@ -153,8 +149,4 @@ test('Test `marginSizes` function', () => {
|
||||
expect(object).toEqual({
|
||||
marginBottom: 15,
|
||||
})
|
||||
|
||||
object = {}
|
||||
marginSizes()(object)
|
||||
expect(object).toEqual(object)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user