Socket
Book a DemoSign in
Socket

@emilgroup/document-uploader

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@emilgroup/document-uploader - npm Package Compare versions

Comparing version
0.0.8
to
0.0.9
.eslintignore

Sorry, the diff of this file is not supported yet

+11
{
"root": true,
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module",
"project": "./tsconfig.json"
},
"plugins": ["@typescript-eslint", "jest"],
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended", "plugin:jest/recommended"]
}
{
"printWidth": 120,
"trailingComma": "all",
"singleQuote": true
}
{
"cSpell.words": ["insuranceservice"]
}
image: node:18.16.0
definitions:
steps:
- step: &lint_and_build
name: 'Lint and build the NestJS package'
caches:
- node
script:
- printf "//`node -p \"require('url').parse('https://registry.npmjs.org').host\"`/:_authToken=$NPM_TOKEN" >> ~/.npmrc
- npm install
- npm run lint
- npm run build
- step: &lint_build_and_publish
name: 'Lint, Build and Publish'
script:
- printf "//`node -p \"require('url').parse('https://registry.npmjs.org').host\"`/:_authToken=$NPM_TOKEN" >> ~/.npmrc
- npm install
- npm run lint
- npm run build
- npm publish
pipelines:
branches:
master:
- step: *lint_build_and_publish
pull-requests:
'*':
- step: *lint_and_build
export declare enum ProductDocumentType {
PreApplication = "pre-application",
PostApplication = "post-application",
PreConfirmation = "pre-confirmation"
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProductDocumentType = void 0;
var ProductDocumentType;
(function (ProductDocumentType) {
ProductDocumentType["PreApplication"] = "pre-application";
ProductDocumentType["PostApplication"] = "post-application";
ProductDocumentType["PreConfirmation"] = "pre-confirmation";
})(ProductDocumentType = exports.ProductDocumentType || (exports.ProductDocumentType = {}));
export declare const logger: import("pino").Logger<never>;
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.logger = void 0;
const pino_1 = __importDefault(require("pino"));
exports.logger = (0, pino_1.default)({
level: process.env.LOG_LEVEL || 'info',
});
export { Environment, ProductDocumentsApi } from '@emilgroup/document-sdk-node';
export * from './uploader/document-uploader';
export * from './common/enums';
export * from './common/logger';
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProductDocumentsApi = exports.Environment = void 0;
var document_sdk_node_1 = require("@emilgroup/document-sdk-node");
Object.defineProperty(exports, "Environment", { enumerable: true, get: function () { return document_sdk_node_1.Environment; } });
Object.defineProperty(exports, "ProductDocumentsApi", { enumerable: true, get: function () { return document_sdk_node_1.ProductDocumentsApi; } });
__exportStar(require("./uploader/document-uploader"), exports);
__exportStar(require("./common/enums"), exports);
__exportStar(require("./common/logger"), exports);
/// <reference types="node" />
import fs from 'fs';
import { Environment } from '@emilgroup/document-sdk-node';
import { ProductDocumentType } from '../common/enums';
export interface PreSignedPostRequest {
templateSlug: string;
entityType: string;
description: string;
requester: string;
filename: string;
policyCode?: string;
leadCode?: string;
accountCode?: string;
productSlug?: string | null;
entityId?: number;
ern?: string;
}
export interface PreSignedPostResponse {
url: string;
fields: Record<string, string>;
}
export interface UploadDocumentResponse {
templateSlug: string;
entityType: string;
description: string;
requester: string;
filename: string;
code: string;
policyCode?: string;
leadCode?: string;
accountCode?: string;
productSlug?: string | null;
entityId?: number;
}
export declare class DocumentUploader {
private readonly email;
private readonly password;
private readonly environment;
private docApi;
private productDocApi;
constructor(email: string, password: string, environment: Environment);
private createPreSignedPost;
uploadProductDocument({ productSlug, file, filename, productDocumentType, entityType, description, productVersionId, }: {
productSlug: string;
file: fs.ReadStream;
filename: string;
productDocumentType: ProductDocumentType;
entityType: string;
description: string;
productVersionId?: number;
}): Promise<Record<string, string>>;
uploadDocument(payload: PreSignedPostRequest, file: fs.ReadStream, size: number): Promise<UploadDocumentResponse>;
}
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DocumentUploader = void 0;
const axios_1 = __importDefault(require("axios"));
const form_data_1 = __importDefault(require("form-data"));
const fs_1 = __importDefault(require("fs"));
const document_sdk_node_1 = require("@emilgroup/document-sdk-node");
const document_sdk_node_2 = require("@emilgroup/document-sdk-node");
const mime = __importStar(require("mime-types"));
const logger_1 = require("../common/logger");
class DocumentUploader {
constructor(email, password, environment) {
this.email = email;
this.password = password;
this.environment = environment;
this.docApi = new document_sdk_node_1.DocumentsApi();
this.productDocApi = new document_sdk_node_2.ProductDocumentsApi();
this.docApi.selectEnvironment(this.environment);
this.productDocApi.selectEnvironment(this.environment);
logger_1.logger.level = process.env.LOG_LEVEL || 'info';
}
async createPreSignedPost(params) {
await this.docApi.authorize(this.email, this.password);
const { data } = await this.docApi.createPresignedPost({
createPresignedPostRequestDto: params,
});
return data;
}
async uploadProductDocument({ productSlug, file, filename, productDocumentType, entityType, description, productVersionId, }) {
const filePath = file.path;
if (typeof filePath !== 'string') {
throw new Error('File path is not a string');
}
const mimeType = mime.contentType(filePath);
if (!mimeType) {
throw new Error('Could not determine mime type');
}
const ext = mime.extension(mimeType);
if (!ext) {
throw new Error('Could not determine file extension');
}
logger_1.logger.debug('file path is %s', filePath);
logger_1.logger.debug(mimeType, 'mime type is %s', mimeType);
logger_1.logger.debug(ext, 'extension is %s', ext);
logger_1.logger.debug(productSlug, 'productSlug = %s ', productSlug);
logger_1.logger.debug('Attempting to authorize productDocApi');
await this.productDocApi.authorize(this.email, this.password);
logger_1.logger.debug('Successfully authorized productDocApi');
const { data } = await this.productDocApi.uploadProductDocument({
productSlug: productSlug,
uploadProductDocumentRequestDto: Object.assign({ productSlug,
description,
entityType,
filename, type: productDocumentType, contentType: ext }, (productVersionId && { productVersionId })),
});
logger_1.logger.debug(`Response from upload product document is ${JSON.stringify(data)}`);
const { url, fields } = data;
const form = new form_data_1.default();
logger_1.logger.debug('Attempting to upload product document to S3');
Object.entries(fields).forEach(([field, value]) => {
form.append(field, value);
});
form.append('file', file, {
knownLength: fs_1.default.statSync(filePath).size,
contentType: mimeType,
});
const response = await axios_1.default.post(url, form, {
headers: form.getHeaders(),
});
return {
code: fields.code,
productSlug,
entityType,
filename,
type: productDocumentType,
};
}
async uploadDocument(payload, file, size) {
const filePath = file.path;
if (typeof filePath !== 'string') {
throw new Error('File path is not a string');
}
const mimeType = mime.contentType(filePath);
if (!mimeType) {
throw new Error('Could not determine mime type');
}
const ext = mime.extension(mimeType);
if (!ext) {
throw new Error('Could not determine file extension');
}
const isoContentType = mimeType;
const contentType = ext;
const preSignedPayload = Object.assign(Object.assign({}, payload), { isoContentType,
contentType });
const { url, fields } = await this.createPreSignedPost(preSignedPayload);
const form = new form_data_1.default();
Object.entries(fields).forEach(([field, value]) => {
form.append(field, value);
});
const appendFileOpts = {
knownLength: size,
};
form.append('file', file, appendFileOpts);
await axios_1.default.post(url, form, {
headers: form.getHeaders(),
});
return Object.assign(Object.assign({}, preSignedPayload), { code: fields.code });
}
}
exports.DocumentUploader = DocumentUploader;
module.exports = {
transform: {
'^.+\\.(t|j)s$': 'ts-jest',
},
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(t|j)s$',
moduleFileExtensions: ['ts', 'js', 'json', 'node'],
};
export enum ProductDocumentType {
PreApplication = 'pre-application',
PostApplication = 'post-application',
PreConfirmation = 'pre-confirmation',
}
import pino from 'pino';
export const logger = pino({
level: process.env.LOG_LEVEL || 'info',
});
export { Environment, ProductDocumentsApi } from '@emilgroup/document-sdk-node';
export * from './uploader/document-uploader';
export * from './common/enums';
export * from './common/logger';
import axios from 'axios';
import FormData from 'form-data';
import fs from 'fs';
import {
CreatePresignedPostRequestDto,
DocumentsApi,
Environment,
UploadProductDocumentRequestDtoContentTypeEnum,
} from '@emilgroup/document-sdk-node';
import { ProductDocumentsApi } from '@emilgroup/document-sdk-node';
import * as mime from 'mime-types';
import { ProductDocumentType } from '../common/enums';
import { logger } from '../common/logger';
export interface PreSignedPostRequest {
templateSlug: string;
entityType: string;
description: string;
requester: string;
filename: string;
policyCode?: string;
leadCode?: string;
accountCode?: string;
productSlug?: string | null;
entityId?: number;
ern?: string;
}
export interface PreSignedPostResponse {
url: string;
fields: Record<string, string>;
}
export interface UploadDocumentResponse {
templateSlug: string;
entityType: string;
description: string;
requester: string;
filename: string;
code: string;
policyCode?: string;
leadCode?: string;
accountCode?: string;
productSlug?: string | null;
entityId?: number;
}
export class DocumentUploader {
private docApi: DocumentsApi;
private productDocApi: ProductDocumentsApi;
constructor(
private readonly email: string,
private readonly password: string,
private readonly environment: Environment,
) {
this.docApi = new DocumentsApi();
this.productDocApi = new ProductDocumentsApi();
this.docApi.selectEnvironment(this.environment);
this.productDocApi.selectEnvironment(this.environment);
logger.level = process.env.LOG_LEVEL || 'info';
}
private async createPreSignedPost(params: CreatePresignedPostRequestDto): Promise<PreSignedPostResponse> {
await this.docApi.authorize(this.email, this.password);
const { data } = await this.docApi.createPresignedPost({
createPresignedPostRequestDto: params,
});
return data as PreSignedPostResponse;
}
public async uploadProductDocument({
productSlug,
file,
filename,
productDocumentType,
entityType,
description,
productVersionId,
}: {
productSlug: string;
file: fs.ReadStream;
filename: string;
productDocumentType: ProductDocumentType;
entityType: string;
description: string;
productVersionId?: number;
}): Promise<Record<string, string>> {
const filePath = file.path;
if (typeof filePath !== 'string') {
throw new Error('File path is not a string');
}
const mimeType = mime.contentType(filePath);
if (!mimeType) {
throw new Error('Could not determine mime type');
}
const ext = mime.extension(mimeType);
if (!ext) {
throw new Error('Could not determine file extension');
}
logger.debug('file path is %s', filePath);
logger.debug(mimeType, 'mime type is %s', mimeType);
logger.debug(ext, 'extension is %s', ext);
logger.debug(productSlug, 'productSlug = %s ', productSlug);
logger.debug('Attempting to authorize productDocApi');
await this.productDocApi.authorize(this.email, this.password);
logger.debug('Successfully authorized productDocApi');
const { data } = await this.productDocApi.uploadProductDocument({
productSlug: productSlug,
uploadProductDocumentRequestDto: {
productSlug,
description,
entityType,
filename,
type: productDocumentType,
contentType: ext as UploadProductDocumentRequestDtoContentTypeEnum,
...(productVersionId && { productVersionId }),
},
});
logger.debug(`Response from upload product document is ${JSON.stringify(data)}`);
const { url, fields } = data as unknown as PreSignedPostResponse;
const form = new FormData();
logger.debug('Attempting to upload product document to S3');
Object.entries(fields).forEach(([field, value]) => {
form.append(field, value);
});
form.append('file', file, {
knownLength: fs.statSync(filePath).size,
contentType: mimeType,
});
const response = await axios.post(url, form, {
headers: form.getHeaders(),
});
return {
code: fields.code,
productSlug,
entityType,
filename,
type: productDocumentType,
};
}
async uploadDocument(
payload: PreSignedPostRequest,
file: fs.ReadStream,
size: number,
): Promise<UploadDocumentResponse> {
const filePath = file.path;
if (typeof filePath !== 'string') {
throw new Error('File path is not a string');
}
const mimeType = mime.contentType(filePath);
if (!mimeType) {
throw new Error('Could not determine mime type');
}
const ext = mime.extension(mimeType);
if (!ext) {
throw new Error('Could not determine file extension');
}
const isoContentType = mimeType;
const contentType = ext;
const preSignedPayload = {
...payload,
isoContentType,
contentType,
} as CreatePresignedPostRequestDto;
const { url, fields } = await this.createPreSignedPost(preSignedPayload);
const form = new FormData();
Object.entries(fields).forEach(([field, value]) => {
form.append(field, value);
});
const appendFileOpts: FormData.AppendOptions = {
knownLength: size,
};
form.append('file', file, appendFileOpts);
await axios.post(url, form, {
headers: form.getHeaders(),
});
return { ...preSignedPayload, code: fields.code };
}
}
{
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"removeComments": true,
"declaration": true,
"outDir": "./dist",
"strict": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"esModuleInterop": true,
"useUnknownInCatchVariables": false
},
"exclude": ["node_modules", "dist"]
}
+13
-16
{
"name": "@emilgroup/document-uploader",
"version": "0.0.8",
"version": "0.0.9",
"description": "TypeScript NPM Module to upload document to EMIL Document Service",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc",
"format": "prettier --write \"src/**/*.(js|ts)\"",
"lint": "eslint src --ext .js,.ts",
"lint:fix": "eslint src --fix --ext .js,.ts",
"format": "prettier --write \\\"lib/**/*.(js|ts)\\\"",
"lint": "eslint lib --ext .js,.ts",
"lint:fix": "eslint lib --fix --ext .js,.ts",
"prepare": "npm run build",
"prepublishOnly": "npm run lint",
"preversion": "npm run lint",
"version": "npm run format && git add -A src",
"postversion": "git push && git push --tags"
"prepublish": "npx tsc",
"preversion": "npm run lint"
},

@@ -26,3 +25,2 @@ "keywords": [

"@types/node": "^18.15.11",
"jest": "^29.5.0",
"@typescript-eslint/eslint-plugin": "5.57.1",

@@ -32,2 +30,3 @@ "@typescript-eslint/parser": "5.57.1",

"eslint-plugin-jest": "27.2.1",
"jest": "^29.5.0",
"prettier": "2.8.7",

@@ -37,15 +36,13 @@ "ts-node": "^10.9.1",

},
"files": [
"lib/**/*"
],
"dependencies": {
"@emilgroup/document-sdk-node": "^1.1.0",
"@emilgroup/document-sdk-node": "^1.6.0",
"@faker-js/faker": "^7.6.0",
"axios": "^1.3.5",
"form-data": "^4.0.0",
"mime-types": "^2.1.35"
"mime-types": "^2.1.35",
"pino": "^8.17.1"
},
"volta": {
"node": "18.15.0"
"node": "18.16.0"
}
}
export { Environment } from '@emilgroup/document-sdk-node';
export * from './interfaces/document.interface';
export * from './uploader/document-uploader';
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Environment = void 0;
var document_sdk_node_1 = require("@emilgroup/document-sdk-node");
Object.defineProperty(exports, "Environment", { enumerable: true, get: function () { return document_sdk_node_1.Environment; } });
__exportStar(require("./interfaces/document.interface"), exports);
__exportStar(require("./uploader/document-uploader"), exports);
export interface PreSignedPostRequest {
templateSlug: string;
entityType: string;
description: string;
requester: string;
filename: string;
policyCode?: string;
leadCode?: string;
accountCode?: string;
productSlug?: string | null;
entityId?: number;
}
export interface PreSignedPostResponse {
url: string;
fields: Record<string, string>;
}
export interface UploadDocumentResponse {
templateSlug: string;
entityType: string;
description: string;
requester: string;
filename: string;
code: string;
policyCode?: string;
leadCode?: string;
accountCode?: string;
productSlug?: string | null;
entityId?: number;
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/// <reference types="node" />
import fs from 'fs';
import { PreSignedPostRequest, UploadDocumentResponse } from '../interfaces/document.interface';
import { Environment } from '@emilgroup/document-sdk-node';
export declare class DocumentUploader {
private readonly email;
private readonly password;
private readonly environment;
private docApi;
constructor(email: string, password: string, environment: Environment);
private createPreSignedPost;
uploadDocument(payload: PreSignedPostRequest, file: fs.ReadStream, size: number): Promise<UploadDocumentResponse>;
}
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DocumentUploader = void 0;
const axios_1 = __importDefault(require("axios"));
const form_data_1 = __importDefault(require("form-data"));
const document_sdk_node_1 = require("@emilgroup/document-sdk-node");
const mime = __importStar(require("mime-types"));
class DocumentUploader {
email;
password;
environment;
docApi;
constructor(email, password, environment) {
this.email = email;
this.password = password;
this.environment = environment;
this.docApi = new document_sdk_node_1.DocumentsApi();
this.docApi.selectEnvironment(this.environment);
}
async createPreSignedPost(params) {
await this.docApi.authorize(this.email, this.password);
const { data } = await this.docApi.createPresignedPost({
createPresignedPostRequestDto: params,
});
return data;
}
async uploadDocument(payload, file, size) {
const filePath = file.path;
if (typeof filePath !== 'string') {
throw new Error('File path is not a string');
}
const mimeType = mime.contentType(filePath);
if (!mimeType) {
throw new Error('Could not determine mime type');
}
const ext = mime.extension(mimeType);
if (!ext) {
throw new Error('Could not determine file extension');
}
const isoContentType = mimeType;
const contentType = ext;
const preSignedPayload = {
...payload,
isoContentType,
contentType,
};
const { url, fields } = await this.createPreSignedPost(preSignedPayload);
const form = new form_data_1.default();
Object.entries(fields).forEach(([field, value]) => {
form.append(field, value);
});
const appendFileOpts = {
knownLength: size,
};
form.append('file', file, appendFileOpts);
await axios_1.default.post(url, form, {
headers: form.getHeaders(),
});
return { ...preSignedPayload, code: fields.code };
}
}
exports.DocumentUploader = DocumentUploader;