Skip to content

Commit

Permalink
added mimeType to result
Browse files Browse the repository at this point in the history
  • Loading branch information
dopecodez committed Sep 14, 2020
1 parent bfa8eec commit cedb4fc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,15 @@ The second argument is the path where you want to save the image. This can be re

The third argument is the filename which can be any valid string.

The method will return the path of the updated image.
The method will return a `base64ImgResult` object type which is exported in `types`.

```js
interface base64ImgResult {
path: string,
mimeType: string
}
```
The mimeType and path parameters can be useful in case you want to save the path or know what type your image was saved as.

The `type` parameter in the `base64ImgOptions` is not required. If specified, it will create an image of the given type. If not present, the type will be inferred from the image type section of the base64 image string. If no type portion exists for base64 string, it will default to `png`.

Expand Down
11 changes: 8 additions & 3 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'path';
import { checkValidDestination, writeToMemory, isImage, readFileAndConvert } from './file';
import { checkIfValidUrl, bufferToString, checkIfValidBase64 } from './base64';
import fetch from 'node-fetch';
import { base64ImgOptions } from './types';
import { base64ImgOptions, base64ImgResult } from './types';

//The initial async convert function
const base64Img = async (base64String: string, destPath: string, fileName: string, options?: base64ImgOptions) => {
Expand All @@ -22,7 +22,11 @@ const base64Img = async (base64String: string, destPath: string, fileName: strin
if (checkValidDestination(filePath)) {
await writeToMemory(fullPath, data);
}
return fullPath;
const result : base64ImgResult = {
path: fullPath,
mimeType: mimeType
}
return result;
} catch (error) {
throw error;
}
Expand All @@ -46,5 +50,6 @@ module.exports.default = base64Img;

// Export types
export {
base64ImgOptions
base64ImgOptions,
base64ImgResult
} from './types';
5 changes: 5 additions & 0 deletions source/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
export interface base64ImgOptions {
type: string
}

export interface base64ImgResult{
path: string,
mimeType: string
}

0 comments on commit cedb4fc

Please sign in to comment.