Skip to content

Commit 2a3eaa9

Browse files
authored
Merge pull request #32 from FusionAuth/fix-form-body-stringification
Fix form body stringification
2 parents 84039af + 6678702 commit 2a3eaa9

File tree

6 files changed

+117
-7
lines changed

6 files changed

+117
-7
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
## FusionAuth TypeScript Client
22
![semver 2.0.0 compliant](http://img.shields.io/badge/semver-2.0.0-brightgreen.svg?style=flat-square) [![npm](https://img.shields.io/npm/v/@fusionauth/typescript-client?style=flat-square)](https://www.npmjs.com/package/@fusionauth/typescript-client)
33

4-
If you're integrating FusionAuth with a Typescript application, this library will speed up your development time.
4+
If you're integrating FusionAuth with a Typescript application, this library will speed up your development time. It also works with node and browser applications as well.
55

66
For additional information and documentation on FusionAuth refer to [https://fusionauth.io](https://fusionauth.io).
77

@@ -18,3 +18,8 @@ npm install @fusionauth/typescript-client
1818
Refer to the FusionAuth API documentation to for request and response formats.
1919
* https://fusionauth.io/docs/v1/tech/apis/
2020
* https://fusionauth.io/docs/v1/tech/client-libraries/typescript
21+
22+
## Development
23+
24+
* Set up a fusionauth instance. (Not sure exactly how to configure, TBD.)
25+
* `sb test`

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
"dist/fusionauth-typescript-client.*"
1919
],
2020
"scripts": {
21-
"test": "npx mocha build/test/FusionAuthClientTest.js && npx karma start",
21+
"test": "npx mocha build/test/*.js && npx karma start",
2222
"build-browser": "mkdir -p dist && npx browserify index.ts --standalone FusionAuth --debug -p [ tsify --target=es5 ] -t browserify-shim -o dist/fusionauth-typescript-client.js",
2323
"build-browser-min": "mkdir -p dist && npx browserify index.ts --standalone FusionAuth --debug -p [ tsify --target=es5 ] -t browserify-shim -t uglifyify | npx uglifyjs --source-map base -o dist/fusionauth-typescript-client.min.js",
24-
"pretest": "npx tsc && npx tsc -p test && mkdir -p dist && npx browserify test/FusionAuthClientTest.ts --debug -t envify -p [ tsify --target=es5 -p test ] -t browserify-shim -t uglifyify | npx uglifyjs --source-map base -o dist/fusionauth-typescript-client-test.min.js",
24+
"pretest": "npx tsc && npx tsc -p test && mkdir -p dist && npx browserify test/*.ts --debug -t envify -p [ tsify --target=es5 -p test ] -t browserify-shim -t uglifyify | npx uglifyjs --source-map base -o dist/fusionauth-typescript-client-test.min.js",
2525
"prepare": "npx tsc && npm run build-browser && npm run build-browser-min"
2626
},
2727
"repository": {

src/DefaultRESTClient.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, FusionAuth, All Rights Reserved
2+
* Copyright (c) 2020, FusionAuth, All Rights Reserved
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -82,6 +82,15 @@ export default class DefaultRESTClient<RT, ERT> implements IRESTClient<RT, ERT>
8282
* @param body The object to be written to the request body as form data.
8383
*/
8484
withFormData(body: URLSearchParams): DefaultRESTClient<RT, ERT> {
85+
const body2 = new URLSearchParams();
86+
if (body) {
87+
body.forEach((value, name, searchParams) => {
88+
if (value && value.length > 0 && value != "null" && value != "undefined") {
89+
body2.set(name,value);
90+
}
91+
});
92+
body = body2;
93+
}
8594
this.body = body;
8695
this.withHeader('Content-Type', 'application/x-www-form-urlencoded');
8796
return this;

test/DefaultRESTClientTest.ts

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright (c) 2020, FusionAuth, All Rights Reserved
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing,
11+
* software distributed under the License is distributed on an
12+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13+
* either express or implied. See the License for the specific
14+
* language governing permissions and limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
import * as chai from 'chai';
20+
import DefaultRESTClient from "../src/DefaultRESTClient"
21+
import {URLSearchParams} from "url";
22+
23+
24+
describe('#DefaultRESTClient()', function () {
25+
26+
it('Can Create DefaultRESTClient', async () => {
27+
const client = new DefaultRESTClient('http://localhost:9011');
28+
chai.assert.isNotNull(client);
29+
});
30+
31+
describe('withFormData', function () {
32+
it('null', async () => {
33+
const client = new DefaultRESTClient('http://localhost:9011');
34+
let body = client.withFormData(null).body;
35+
chai.assert.isNull(body);
36+
});
37+
38+
it('empty', async () => {
39+
const client = new DefaultRESTClient('http://localhost:9011');
40+
let params = new URLSearchParams();
41+
let body = client.withFormData(params).body;
42+
chai.assert.isNotNull(body);
43+
chai.assert.strictEqual(body.toString(), "");
44+
});
45+
46+
it('with one value', async () => {
47+
const client = new DefaultRESTClient('http://localhost:9011');
48+
let params = new URLSearchParams();
49+
params.set('key','value');
50+
let body = client.withFormData(params).body;
51+
chai.assert.isNotNull(body);
52+
chai.assert.strictEqual(body.toString(), "key=value");
53+
});
54+
55+
it('with two values', async () => {
56+
const client = new DefaultRESTClient('http://localhost:9011');
57+
let params = new URLSearchParams();
58+
params.set('key','value');
59+
params.set('key2','value2');
60+
let body = client.withFormData(params).body;
61+
chai.assert.isNotNull(body);
62+
chai.assert.strictEqual(body.toString(), "key=value&key2=value2");
63+
});
64+
65+
it('skips undefined value', async () => {
66+
const client = new DefaultRESTClient('http://localhost:9011');
67+
let params = new URLSearchParams();
68+
params.set('key','value');
69+
params.set('key2',undefined);
70+
let body = client.withFormData(params).body;
71+
chai.assert.isNotNull(body);
72+
chai.assert.strictEqual(body.toString(), "key=value");
73+
});
74+
75+
it('skips null value', async () => {
76+
const client = new DefaultRESTClient('http://localhost:9011');
77+
let params = new URLSearchParams();
78+
params.set('key','value');
79+
params.set('key2',null);
80+
let body = client.withFormData(params).body;
81+
chai.assert.isNotNull(body);
82+
chai.assert.strictEqual(body.toString(), "key=value");
83+
});
84+
85+
it('sets content type', async () => {
86+
const client = new DefaultRESTClient('http://localhost:9011');
87+
let params = new URLSearchParams();
88+
let headers = client.withFormData(params).headers
89+
chai.assert.isNotNull(headers);
90+
chai.assert.strictEqual(headers['Content-Type'], "application/x-www-form-urlencoded");
91+
});
92+
93+
});
94+
95+
});

test/tsconfig.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"extends": "../tsconfig.json",
33
"files": [
4-
"FusionAuthClientTest.ts"
4+
"FusionAuthClientTest.ts",
5+
"DefaultRESTClientTest.ts"
56
],
67
"compilerOptions": {
78
"types": [
@@ -11,4 +12,4 @@
1112
"chai"
1213
]
1314
}
14-
}
15+
}

0 commit comments

Comments
 (0)