dispatch-node-sdk
Advanced tools
Comparing version 0.0.24 to 0.0.25
@@ -47,2 +47,8 @@ 'use strict'; | ||
var _formData = require('form-data'); | ||
var _formData2 = _interopRequireDefault(_formData); | ||
var _fileHelpers = require('./fileHelpers'); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } | ||
@@ -63,2 +69,6 @@ | ||
this.host = host; | ||
// Determine files api url from host | ||
this.filesAPIHost = this.host.replace('api', 'files-api'); | ||
this.session = null; | ||
@@ -103,2 +113,15 @@ this.entities = { | ||
}, { | ||
key: 'getFilesAPIClient', | ||
value: function getFilesAPIClient() { | ||
if (!this.filesAPIClient) { | ||
this.filesAPIClient = new _rawClient2.default({ | ||
authMode: _rawClient.AUTH_MODE_BEARER, | ||
bearerToken: this.bearerToken, | ||
host: this.filesAPIHost | ||
}); | ||
} | ||
return this.filesAPIClient; | ||
} | ||
}, { | ||
key: 'getAppClient', | ||
@@ -311,4 +334,12 @@ value: function getAppClient() { | ||
var req = new _request2.default(method, this.urlWithSession(endpoint), body, options); | ||
var client = void 0; | ||
if (options.file) { | ||
client = this.getFilesAPIClient(); | ||
} else { | ||
client = this.getAuthClient(); | ||
} | ||
return new Promise(function (resolve, reject) { | ||
req.do(_this6.getAuthClient()).then(resolve).catch(function (err) { | ||
req.do(client).then(resolve).catch(function (err) { | ||
if (err instanceof _errors.UnauthorizedError) { | ||
@@ -350,2 +381,25 @@ if (_this6.refreshToken) { | ||
/** | ||
* Upload a file to the files API | ||
* @param {String} file The base64 data for the file | ||
* @param {String} name The file name | ||
* @return {Promise} | ||
*/ | ||
}, { | ||
key: 'uploadFile', | ||
value: function uploadFile(file, name) { | ||
var type = arguments.length <= 2 || arguments[2] === undefined ? 'square_photo' : arguments[2]; | ||
var formData = new _formData2.default(); | ||
formData.append('file', (0, _fileHelpers.dataURItoBlob)(file), name); | ||
formData.append('filename', name); | ||
formData.append('type', type); | ||
return this.doAuthenticatedRequest('POST', '/v1/datafiles', formData, { | ||
file: true, | ||
headers: {} | ||
}); | ||
} | ||
/** | ||
* This adds query parameters to every request out to the API so it | ||
@@ -352,0 +406,0 @@ * can add them to events, etc. For use with analytics. |
@@ -59,2 +59,14 @@ 'use strict'; | ||
addPhoto: function addPhoto(fileToken, name, description) { | ||
return client.doAuthenticatedRequest('POST', endpoints.ATTACHMENTS, { | ||
entity_type: 'Job', | ||
entity_id: id, | ||
description: description, | ||
name: name, | ||
file_token: fileToken | ||
}).then(function (response) { | ||
return response.attachment; | ||
}); | ||
}, | ||
createAppointment: function createAppointment() { | ||
@@ -61,0 +73,0 @@ var data = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; |
@@ -130,3 +130,3 @@ 'use strict'; | ||
var headers = { | ||
var headers = options.headers || { | ||
'Content-Type': 'application/json', | ||
@@ -159,3 +159,7 @@ Accept: 'application/json' | ||
if (body) { | ||
params.body = JSON.stringify(body); | ||
if (headers['Content-Type'] === 'application/json') { | ||
params.body = JSON.stringify(body); | ||
} else { | ||
params.body = body; | ||
} | ||
} | ||
@@ -162,0 +166,0 @@ |
@@ -11,2 +11,5 @@ import _ from 'underscore'; | ||
import { UnauthorizedError } from './errors'; | ||
import FormData from 'form-data'; | ||
import { dataURItoBlob } from './fileHelpers'; | ||
export default class Dispatch { | ||
@@ -17,2 +20,6 @@ constructor(clientID, clientSecret, host = 'https://api.dispatch.me') { | ||
this.host = host; | ||
// Determine files api url from host | ||
this.filesAPIHost = this.host.replace('api', 'files-api'); | ||
this.session = null; | ||
@@ -54,2 +61,14 @@ this.entities = { | ||
getFilesAPIClient() { | ||
if (!this.filesAPIClient) { | ||
this.filesAPIClient = new RawClient({ | ||
authMode: AUTH_MODE_BEARER, | ||
bearerToken: this.bearerToken, | ||
host: this.filesAPIHost, | ||
}); | ||
} | ||
return this.filesAPIClient; | ||
} | ||
getAppClient() { | ||
@@ -204,4 +223,12 @@ if (!this.appClient) { | ||
const req = new Request(method, this.urlWithSession(endpoint), body, options); | ||
let client; | ||
if (options.file) { | ||
client = this.getFilesAPIClient(); | ||
} else { | ||
client = this.getAuthClient(); | ||
} | ||
return new Promise((resolve, reject) => { | ||
req.do(this.getAuthClient()) | ||
req.do(client) | ||
.then(resolve) | ||
@@ -241,3 +268,21 @@ .catch(err => { | ||
/** | ||
* Upload a file to the files API | ||
* @param {String} file The base64 data for the file | ||
* @param {String} name The file name | ||
* @return {Promise} | ||
*/ | ||
uploadFile(file, name, type = 'square_photo') { | ||
const formData = new FormData(); | ||
formData.append('file', dataURItoBlob(file), name); | ||
formData.append('filename', name); | ||
formData.append('type', type); | ||
return this.doAuthenticatedRequest('POST', '/v1/datafiles', formData, { | ||
file: true, | ||
headers: {}, | ||
}); | ||
} | ||
/** | ||
@@ -244,0 +289,0 @@ * This adds query parameters to every request out to the API so it |
@@ -255,2 +255,12 @@ import nock from 'nock'; | ||
}); | ||
it('should hit the files api if options.file is true', () => { | ||
const client = new Dispatch(testClientID, testClientSecret); | ||
const scope = nock('https://files-api.dispatch.me').get('/').reply(200); | ||
client.setBearerToken(testBearerToken); | ||
client.doAuthenticatedRequest('GET', '/', null, { | ||
file: true, | ||
}); | ||
expect(scope.isDone()).toEqual(true); | ||
}); | ||
}); | ||
@@ -263,2 +273,20 @@ | ||
}); | ||
describe('uploadFile', () => { | ||
it('should upload to correct URL, but blob on server is BS'); | ||
/** | ||
const client = new Dispatch(testClientID, testClientSecret); | ||
nock('https://files-api.dispatch.me').post('/v1/datafiles').reply(201, { | ||
datafile: { | ||
uid: '1234-5678', | ||
}, | ||
}); | ||
client.setBearerToken(testBearerToken); | ||
client.uploadFile('data:image/jpeg;base64,/9j/4AAQSkZJRgABAAEAYABgAADACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKAP/2Q==', 'photo.jpg').then(response => { | ||
console.log(response); | ||
done(); | ||
}).catch(done); | ||
**/ | ||
}); | ||
}); |
@@ -50,7 +50,15 @@ import * as endpoints from '../endpoints'; | ||
description: text, | ||
}).then(response => { | ||
return response.attachment; | ||
}); | ||
}).then(response => response.attachment); | ||
}, | ||
addPhoto: (fileToken, name, description) => { | ||
return client.doAuthenticatedRequest('POST', endpoints.ATTACHMENTS, { | ||
entity_type: 'Job', | ||
entity_id: id, | ||
description, | ||
name, | ||
file_token: fileToken, | ||
}).then(response => response.attachment); | ||
}, | ||
createAppointment: (data = {}) => { | ||
@@ -57,0 +65,0 @@ return client.doAuthenticatedRequest('POST', endpoints.APPOINTMENTS, Object.assign({}, data, { |
@@ -162,2 +162,20 @@ import expect from 'expect'; | ||
}); | ||
describe('addPhoto', () => { | ||
it('should call endpoint with correct body', () => { | ||
const client = new Dispatch(testClientID, testClientSecret); | ||
client.setBearerToken(testBearerToken, testRefreshToken); | ||
const scope = nock('https://api.dispatch.me') | ||
.post(`${endpoints.ATTACHMENTS}`, { | ||
entity_type: 'Job', | ||
entity_id: 123, | ||
file_token: '12345', | ||
name: 'test name', | ||
description: 'test description', | ||
}) | ||
.reply(201); | ||
client.entities.job(123).addPhoto('12345', 'test name', 'test description'); | ||
expect(scope.isDone()).toEqual(true); | ||
}); | ||
}); | ||
}); |
@@ -90,3 +90,3 @@ import { ensure, oneOf } from 'simplecheck'; | ||
const headers = { | ||
const headers = options.headers || { | ||
'Content-Type': 'application/json', | ||
@@ -119,3 +119,7 @@ Accept: 'application/json', | ||
if (body) { | ||
params.body = JSON.stringify(body); | ||
if (headers['Content-Type'] === 'application/json') { | ||
params.body = JSON.stringify(body); | ||
} else { | ||
params.body = body; | ||
} | ||
} | ||
@@ -122,0 +126,0 @@ |
{ | ||
"name": "dispatch-node-sdk", | ||
"version": "0.0.24", | ||
"version": "0.0.25", | ||
"description": "High- and low-level libraries for interacting with the Dispatch API", | ||
@@ -36,2 +36,3 @@ "main": "dist/lib/index.js", | ||
"dependencies": { | ||
"form-data": "^1.0.0-rc4", | ||
"isomorphic-fetch": "^2.2.1", | ||
@@ -38,0 +39,0 @@ "moment": "^2.13.0", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
110634
42
2847
5
+ Addedform-data@^1.0.0-rc4
+ Addedasync@2.6.4(transitive)
+ Addedcombined-stream@1.0.8(transitive)
+ Addeddelayed-stream@1.0.0(transitive)
+ Addedform-data@1.0.1(transitive)
+ Addedlodash@4.17.21(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)