Skip to content

Commit

Permalink
Add double conversion check (issue #14)
Browse files Browse the repository at this point in the history
  • Loading branch information
yumauri committed May 30, 2020
1 parent 3d9392b commit cea090f
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
{
"path": "pkg/dist-node/index.js",
"webpack": false,
"limit": "3916 B"
"limit": "4100 B"
}
],
"@pika/pack": {
Expand Down
2 changes: 1 addition & 1 deletion src/_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export type HttpHeaders = {
/// request types //////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

export const enum RequestType {
export enum RequestType {
Url,
Ping,
Html,
Expand Down
6 changes: 0 additions & 6 deletions src/internal/source-converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,6 @@ export const toTuples = (source: Source, recursive = false): TupleSource[] => {
return ret
}

// if we get there inside of recursion -> this is bad
if (recursive) {
// by tests looks like this is impossible case -> should remove it?
throw new Error('Bad source, possible recursive iterables?')
}

// if iterable source
if (isIterable(source)) {
const ret: TupleSource[] = []
Expand Down
16 changes: 12 additions & 4 deletions src/internal/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ export const type: {
(type: RequestType.Merge): (request: Request) => MergeRequest
(type: RequestType.Office): (request: Request) => OfficeRequest
(type: RequestType.Markdown): (request: Request) => MarkdownRequest
} = (type: RequestType) => (request: Request): any => ({
...request,
type,
})
} = (type: RequestType) => (request: Request): any => {
if ('type' in request && request.type !== RequestType.Undefined) {
throw new Error(
`Cannot set "${RequestType[type]}" conversion, already set to "${RequestType[request.type]}"`
)
}

return {
...request,
type,
}
}
13 changes: 13 additions & 0 deletions test/internal/source-converters.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ test('Test `toTuples` function', () => {
expect(() => toTuples(set)).toThrow('Bad source, don\'t know what to do with "[object Set]"')
})

test('Test `toTuples` function, different edge cases', () => {
// line 46
const file = createReadStream(`${__dirname}/../manual/statement.html`)
expect(toTuples(file)).toEqual([['index.html', file]])

// line 66, `hasOwnProperty`
function Src(this: any) {
this['index.html'] = 'test'
}
Src.prototype['header.html'] = ''
expect(toTuples(new Src())).toEqual([['index.html', 'test']])
})

test('Test `fromFile` function', () => {
expect(fromFile('file:' + __filename) instanceof ReadStream).toBe(true)
expect(fromFile('file://' + __filename) instanceof ReadStream).toBe(true)
Expand Down
6 changes: 6 additions & 0 deletions test/internal/type.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,9 @@ test('Should change type to Markdown', () => {
type: RequestType.Markdown,
})
})

test('Should throw on double conversion', () => {
const request = { type: RequestType.Url } as Request
const fn = type(RequestType.Office)
expect(() => fn(request)).toThrow(`Cannot set "Office" conversion, already set to "Url"`)
})
7 changes: 7 additions & 0 deletions test/manual/test_issue_14.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { convert, gotenberg, office, pipe, please, url } from '../../src'

// following should throw an exception about double conversion
// >>> Cannot set "Office" conversion, already set to "Url"

const toPDF = pipe(gotenberg('http://localhost:8008'), convert, url, office, please)
toPDF('http://any.url.com').then(console.log).catch(console.error)

0 comments on commit cea090f

Please sign in to comment.