diff --git a/package.json b/package.json index 68916b6..ba9e862 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,7 @@ "tslint-config-standard": "^8.0.1", "typescript": "^3.0.3" }, - "dependencies": { - "jwt-simple": "^0.5.6" + "peerDependencies": { + "jwt-simple": "*" } } diff --git a/rollup.config.ts b/rollup.config.ts index b2c9a6b..3c7dc4c 100644 --- a/rollup.config.ts +++ b/rollup.config.ts @@ -14,7 +14,7 @@ export default { { file: pkg.module, format: 'es', sourcemap: true }, ], // Indicate here external modules you don't wanna include in your bundle (i.e.: 'lodash') - external: [], + external: ['jwt-simple'], watch: { include: 'src/**', }, diff --git a/src/snowflake-ingest-node.ts b/src/snowflake-ingest-node.ts index f8093ac..4ac8387 100644 --- a/src/snowflake-ingest-node.ts +++ b/src/snowflake-ingest-node.ts @@ -1,7 +1,7 @@ import * as crypto from 'crypto'; import { ClientRequest, IncomingMessage } from 'http'; -import https, { RequestOptions } from 'https' // chosen over ie: axios to not bring in extra dep. -import jwt from 'jwt-simple'; +import * as https from 'https'; +import * as jwt from 'jwt-simple'; const USER_AGENT = 'snowpipe-ingest-node/0.0.1/node/npm'; @@ -16,7 +16,7 @@ export type RecordedCallResponse = { } export type RecordedCall = { - request: RequestOptions + request: https.RequestOptions response: RecordedCallResponse } @@ -70,11 +70,15 @@ export const createSnowpipeAPI = (username: string, privateKey: string, account: [EXPIRY_TIME]: Math.round(new Date().getTime() / 1000 + 60 * 59) } + if (jwt === undefined) { + console.error('"jwt-simple" not found (make sure to include this peer dependency in your project).') + } + const bearer = jwt.encode(payload, privateKey, 'RS256'); return bearer; } - const makeRequest = async (options: RequestOptions, endpointCallHistory: RecordedCall[], postBody?: string): Promise => { + const makeRequest = async (options: https.RequestOptions, endpointCallHistory: RecordedCall[], postBody?: string): Promise => { return new Promise((resolve, reject) => { const req: ClientRequest = https.request( options, @@ -145,9 +149,9 @@ export const createSnowpipeAPI = (username: string, privateKey: string, account: const path = `/v1/data/pipes/${pipeName}/insertFiles?requestId=${getRequestId()}`; - const jwt_token: string = await getBearerToken(); + const jwtToken: string = await getBearerToken(); - const options = { + const options: https.RequestOptions = { hostname: config.hostname, port: 443, path, @@ -155,7 +159,7 @@ export const createSnowpipeAPI = (username: string, privateKey: string, account: headers: { 'Content-Type': 'application/json', 'Content-Length': postBody.length, - 'Authorization': `Bearer ${jwt_token}`, + 'Authorization': `Bearer ${jwtToken}`, 'User-Agent': USER_AGENT, Accept: 'application/json' } @@ -173,15 +177,15 @@ export const createSnowpipeAPI = (username: string, privateKey: string, account: path += `&beginMark=${beginMark}`; } - const jwt_token: string = await getBearerToken(); + const jwtToken: string = await getBearerToken(); - const options = { + const options: https.RequestOptions = { hostname: config.hostname, port: 443, path, method: 'GET', headers: { - 'Authorization': `Bearer ${jwt_token}`, + 'Authorization': `Bearer ${jwtToken}`, 'User-Agent': USER_AGENT, Accept: 'application/json' } @@ -202,15 +206,15 @@ export const createSnowpipeAPI = (username: string, privateKey: string, account: path += `&endTimeExclusive=${endTimeExclusive}`; } - const jwt_token: string = await getBearerToken(); + const jwtToken: string = await getBearerToken(); - const options = { + const options: https.RequestOptions = { hostname: config.hostname, port: 443, path, method: 'GET', headers: { - 'Authorization': `Bearer ${jwt_token}`, + 'Authorization': `Bearer ${jwtToken}`, 'User-Agent': USER_AGENT, Accept: 'application/json' }