Skip to content

Commit f3d711a

Browse files
authored
Merge pull request #18 from blinkmobile/ON-2282
ON-2282 # Added generateSubmissionDataUrl function to Forms class
2 parents 33b4806 + 8777a5b commit f3d711a

File tree

12 files changed

+136
-33
lines changed

12 files changed

+136
-33
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
{
2+
"javascript.format.insertSpaceBeforeFunctionParenthesis": true
23
}

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Added
6+
7+
- `forms.generateSubmissionDataUrl()` function
8+
59
## 0.2.3 (2018-10-17)
610

711
### Added

lib/forms.js renamed to classes/Forms.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import type {
1111
} from '../types.js'
1212
*/
1313

14-
const generateFormUrl = require('./generate-form-url.js')
15-
const generateJWT = require('./generate-jwt.js')
16-
const submissionData = require('./retrieve-submission-data.js')
17-
const OneBlinkAPI = require('./one-blink-api.js')
18-
const setPreFillData = require('./pre-fill-data')
14+
const generateFormUrl = require('../lib/generate-form-url.js')
15+
const generateJWT = require('../lib/generate-jwt.js')
16+
const submissionData = require('../lib/retrieve-submission-data.js')
17+
const OneBlinkAPI = require('../lib/one-blink-api.js')
18+
const setPreFillData = require('../lib/pre-fill-data')
1919

2020
module.exports = class Forms extends OneBlinkAPI {
2121
constructor (
@@ -47,7 +47,7 @@ module.exports = class Forms extends OneBlinkAPI {
4747

4848
let preFillFormDataId
4949
if (preFillData) {
50-
const preFillMeta = await super.getRequest(`/forms/${formId}/pre-fill-credentials`)
50+
const preFillMeta = await super.postRequest(`/forms/${formId}/pre-fill-credentials`)
5151
await setPreFillData(preFillMeta, preFillData)
5252
preFillFormDataId = preFillMeta.preFillFormDataId
5353
}
@@ -66,6 +66,24 @@ module.exports = class Forms extends OneBlinkAPI {
6666
}
6767
}
6868

69+
async generateSubmissionDataUrl (
70+
formId /* : ?mixed */,
71+
submissionId /* : ?mixed */,
72+
expiryInSeconds /* : ?mixed */
73+
) /* : Promise<{ url: string }> */ {
74+
if (typeof formId !== 'number') {
75+
return Promise.reject(new TypeError('Must supply "formId" as a number'))
76+
}
77+
if (typeof submissionId !== 'string') {
78+
return Promise.reject(new TypeError('Must supply "submissionId" as a string'))
79+
}
80+
if (typeof expiryInSeconds !== 'number') {
81+
return Promise.reject(new TypeError('Must supply "expiryInSeconds" as a number'))
82+
}
83+
84+
return super.postRequest(`/forms/${formId}/retrieval-url/${submissionId}?expirySeconds=${expiryInSeconds}`)
85+
}
86+
6987
getSubmissionData (
7088
formId /* : ?mixed */,
7189
submissionId /* : ?mixed */
@@ -77,7 +95,7 @@ module.exports = class Forms extends OneBlinkAPI {
7795
return Promise.reject(new TypeError('Must supply "submissionId" as a string'))
7896
}
7997

80-
return super.getRequest(`/forms/${formId}/retrieval-credentials/${submissionId}`)
98+
return super.postRequest(`/forms/${formId}/retrieval-credentials/${submissionId}`)
8199
.then((credentials) => submissionData.getSubmissionData(credentials))
82100
}
83101

lib/keys.js renamed to classes/Keys.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type {
88
} from '../types.js'
99
*/
1010

11-
const OneBlinkAPI = require('./one-blink-api.js')
11+
const OneBlinkAPI = require('../lib/one-blink-api.js')
1212

1313
module.exports = class Keys extends OneBlinkAPI {
1414
constructor (

lib/organisations.js renamed to classes/Organisations.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type {
88
} from '../types.js'
99
*/
1010

11-
const OneBlinkAPI = require('./one-blink-api.js')
11+
const OneBlinkAPI = require('../lib/one-blink-api.js')
1212

1313
module.exports = class Organisations extends OneBlinkAPI {
1414
constructor (

docs/forms.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,37 @@ forms.generateFormUrl(formId, externalId, preFilledData)
5454
}
5555
```
5656

57+
## Generate Submission Data URL
58+
59+
### Example
60+
61+
```javascript
62+
const formId = 1
63+
const submissionId = 'c1f0f27b-4289-4ce5-9807-bf84971991aa'
64+
const expiryInSeconds = 300
65+
forms.generateSubmissionDataUrl(formId, submissionId, expiryInSeconds)
66+
.then((result) => {
67+
const submissionDataUrl = result.url
68+
// Use URL here...
69+
})
70+
```
71+
72+
### Parameters
73+
74+
| Parameter | Required | Type | Description
75+
|---|---|---|---|
76+
| `formId` | Yes | `number` | The exact id of the form you wish to generate a URL for |
77+
| `submissionId` | Yes | `string` | The submission identifier generated after a successful form submission, this will be return to you after a successful forms submission via a callback URL |
78+
| `expiryInSeconds` | Yes | `number` | The number of seconds the signed URL should be valid for |
79+
80+
### Result (Resolved Promise)
81+
82+
```json
83+
{
84+
"url": "https://domain.io/path?query=string",
85+
}
86+
```
87+
5788
## Get Submission Data
5889

5990
### Example
@@ -75,7 +106,7 @@ forms.getSubmissionData(formId, submissionId)
75106

76107
| Parameter | Required | Type | Description
77108
|---|---|---|---|
78-
| `formId` | Yes | `number` | The exact id of the form you wish to generate a URL for |
109+
| `formId` | Yes | `number` | The exact id of the form you wish to get submission data for |
79110
| `submissionId` | Yes | `string` | The submission identifier generated after a successful form submission, this will be return to you after a successful forms submission via a callback URL |
80111

81112
### Result (Resolved Promise)

index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// @flow
22
'use strict'
33

4-
const Forms = require('./lib/forms.js')
5-
const Keys = require('./lib/keys.js')
6-
const Organisations = require('./lib/organisations.js')
4+
const Forms = require('./classes/Forms.js')
5+
const Keys = require('./classes/Keys.js')
6+
const Organisations = require('./classes/Organisations.js')
77

88
module.exports = {
99
Forms,

lib/one-blink-api.js

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
/* ::
55
import type {
6-
Axios
6+
Axios,
7+
$AxiosError
78
} from 'axios'
89
*/
910

@@ -13,6 +14,27 @@ const axios = require('axios')
1314

1415
const generateJWT = require('./generate-jwt.js')
1516

17+
function errorHandler (
18+
error /* : $AxiosError<{
19+
error: string,
20+
message: string,
21+
statusCode: number
22+
}> */
23+
) {
24+
if (error.response) {
25+
// The request was made and the server responded with a status code
26+
// that falls out of the range of 2xx
27+
error.response.data = error.response.data || {
28+
message: 'OneBlink API Internal Server Error'
29+
}
30+
const err = new Error(error.response.data.message)
31+
err.name = 'OneBlinkAPIError'
32+
throw err
33+
}
34+
35+
throw error
36+
}
37+
1638
module.exports = class OneBlinkAPI {
1739
/* ::
1840
oneBlinkAPI: Axios
@@ -50,20 +72,7 @@ module.exports = class OneBlinkAPI {
5072
}
5173
})
5274
.then((response) => response.data)
53-
.catch((error) => {
54-
if (error.response) {
55-
// The request was made and the server responded with a status code
56-
// that falls out of the range of 2xx
57-
error.response.data = error.response.data || {
58-
message: 'OneBlink API Internal Server Error'
59-
}
60-
const err = new Error(error.response.data.message)
61-
err.name = 'OneBlinkAPIError'
62-
throw err
63-
}
64-
65-
throw error
66-
})
75+
.catch(errorHandler)
6776
}
6877

6978
searchRequest (
@@ -73,4 +82,20 @@ module.exports = class OneBlinkAPI {
7382
const search = querystring.stringify(searchParams || {})
7483
return this.getRequest(`${path}?${search}`)
7584
}
85+
86+
postRequest (
87+
path /* : string */,
88+
body /* : ?mixed */
89+
) /* Promise<Object> */ {
90+
// 5 minute expiry
91+
const jwtExpiry = 300
92+
93+
return this.oneBlinkAPI.post(path, body, {
94+
headers: {
95+
'Authorization': `Bearer ${generateJWT(this.accessKey, this.secretKey, jwtExpiry)}`
96+
}
97+
})
98+
.then((response) => response.data)
99+
.catch(errorHandler)
100+
}
76101
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
},
3636
"files": [
3737
"lib",
38+
"classes",
3839
"index.js",
3940
"types.js"
4041
],

tests/lib/forms.test.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe('Forms SDK Class', () => {
3232
}
3333
})
3434

35-
const Forms = require('../../lib/forms.js')
35+
const Forms = require('../../classes/Forms.js')
3636
const forms = new Forms({
3737
accessKey: '123',
3838
secretKey: 'abc'
@@ -80,7 +80,7 @@ describe('Forms SDK Class', () => {
8080
})
8181

8282
describe('getSubmissionData()', () => {
83-
const Forms = require('../../lib/forms.js')
83+
const Forms = require('../../classes/Forms.js')
8484
const forms = new Forms({
8585
accessKey: '123',
8686
secretKey: 'abc',
@@ -98,8 +98,31 @@ describe('Forms SDK Class', () => {
9898
})
9999
})
100100

101+
describe('generateSubmissionDataUrl()', () => {
102+
const Forms = require('../../classes/Forms.js')
103+
const forms = new Forms({
104+
accessKey: '123',
105+
secretKey: 'abc',
106+
oneBlinkAPIOrigin: 'https://domain.api.com'
107+
})
108+
109+
describe('should reject with correct validation errors for', () => {
110+
test('"formId"', () => {
111+
return expect(forms.generateSubmissionDataUrl()).rejects.toThrow('Must supply "formId" as a number')
112+
})
113+
114+
test('"submissionId"', () => {
115+
return expect(forms.generateSubmissionDataUrl(1)).rejects.toThrow('Must supply "submissionId" as a string')
116+
})
117+
118+
test('"expiryInSeconds"', () => {
119+
return expect(forms.generateSubmissionDataUrl(1, '123')).rejects.toThrow('Must supply "expiryInSeconds" as a number')
120+
})
121+
})
122+
})
123+
101124
describe('getForm()', () => {
102-
const Forms = require('../../lib/forms.js')
125+
const Forms = require('../../classes/Forms.js')
103126
const forms = new Forms({
104127
accessKey: '123',
105128
secretKey: 'abc',

0 commit comments

Comments
 (0)