Skip to content

Commit

Permalink
Removed boolean convertOptions and added the migration guide
Browse files Browse the repository at this point in the history
  • Loading branch information
mskec committed Aug 6, 2023
1 parent 0b22a92 commit f4f2f73
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 19 deletions.
19 changes: 19 additions & 0 deletions migration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Migration Guide

## Upgrading from v2.x to v3.x

From the v3 we are deprecating boolean `convertOptions`.

In v2.x, you could get a response in base64 format by providing `true` as the 2nd parameter to the convert function:
```javascript
const convert = fromPath(filePath, options);
const base64Response = convert(page, true);
```

In v3.x, you need to change it to `{ responseType: 'base64' }`, like so:
```javascript
const convert = fromPath(filePath, options);
const base64Response = convert(page, { responseType: 'base64' })
```

The same migration applies for convert `fromBuffer` and `fromBase64` functions.
10 changes: 5 additions & 5 deletions src/types/convert.d.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import type { BufferResponse, ToBase64Response, WriteImageResponse } from './convertResponse';

export type ResponseType = 'image' | 'base64' | 'buffer'
export type ConvertOptions = boolean | {
export type ConvertOptions = {
responseType: ResponseType
}

export type Convert = {
(pages?: number, options?: undefined): Promise<WriteImageResponse>;
(pages: number, options: false | { responseType?: undefined }): Promise<WriteImageResponse>;
(pages: number, options: true | { responseType: 'base64' }): Promise<ToBase64Response>;
(pages: number, options: { responseType?: undefined }): Promise<WriteImageResponse>;
(pages: number, options: { responseType: 'image' }): Promise<WriteImageResponse>;
(pages: number, options: { responseType: 'base64' }): Promise<ToBase64Response>;
(pages: number, options: { responseType: 'buffer' }): Promise<BufferResponse>;

bulk: {
(pages?: number | number[], options?: undefined): Promise<WriteImageResponse[]>;
(pages: number | number[], options: false | { responseType?: undefined }): Promise<WriteImageResponse[]>;
(pages: number | number[], options: true | { responseType: 'base64' }): Promise<ToBase64Response[]>;
(pages: number | number[], options: { responseType?: undefined }): Promise<WriteImageResponse[]>;
(pages: number | number[], options: { responseType: 'image' }): Promise<WriteImageResponse[]>;
(pages: number | number[], options: { responseType: 'base64' }): Promise<ToBase64Response[]>;
(pages: number | number[], options: { responseType: 'buffer' }): Promise<BufferResponse[]>;
};

Expand Down
7 changes: 2 additions & 5 deletions src/utils/resolveResponseType.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { ConvertOptions, ResponseType } from '@module/types/convert';

export const resolveResponseType = (convertOptions?: ConvertOptions): ResponseType => {
if (convertOptions === undefined || typeof convertOptions === 'boolean') {
return convertOptions ? 'base64' : 'image'
}
if (typeof convertOptions !== 'object') {
if (convertOptions && typeof convertOptions !== 'object') {
throw new Error(`Invalid convertOptions type: ${convertOptions}`)
}
return convertOptions.responseType ?? 'image'
return convertOptions?.responseType ?? 'image'
}
2 changes: 1 addition & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ describe("PDF2Pic Core", () => {
}

const convert = fromPath("./test/data/pdf1.pdf", options);
const base64Response = await convert(2, true);
const base64Response = await convert(2, { responseType: 'base64' });

expectBase64ResponseToBeValid(base64Response)
writeFileSync("./dump/fromfiletest/frombase64.png", Buffer.from(base64Response.base64, "base64"));
Expand Down
17 changes: 9 additions & 8 deletions test/resolveResponseType.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,25 @@ describe('resolveResponseType', () => {
expect(resolveResponseType()).to.equal('image')
});

it('should resolve to image if convertOptions is false', () => {
expect(resolveResponseType(false)).to.equal('image')
});

it('should resolve to image if responseType is image', () => {
expect(resolveResponseType({ responseType: 'image' })).to.equal('image')
})

it('should resolve to image if convertOptions is true', () => {
expect(resolveResponseType(true)).to.equal('base64')
it('should resolve to base64 if responseType is base64', () => {
expect(resolveResponseType({ responseType: 'base64' })).to.equal('base64')
});

it('should resolve to base64 if responseType is base64', () => {
expect(resolveResponseType({ responseType: 'image' })).to.equal('image')
it('should resolve to buffer if responseType is buffer', () => {
expect(resolveResponseType({ responseType: 'buffer' })).to.equal('buffer')
});

it('should throw an error if convertOptions is invalid type', async () => {
// @ts-ignore
expect(() => resolveResponseType(1)).to.throw()
});

it('should throw an error if convertOptions is invalid responseType', async () => {
// @ts-ignore
expect(() => resolveResponseType('invalid-type')).to.throw()
});
})

0 comments on commit f4f2f73

Please sign in to comment.