diff --git a/README.md b/README.md index 520aa4d..d8f1aff 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ ## FusionAuth TypeScript Client ![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) -If you're integrating FusionAuth with a Typescript application, this library will speed up your development time. +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. For additional information and documentation on FusionAuth refer to [https://fusionauth.io](https://fusionauth.io). @@ -18,3 +18,8 @@ npm install @fusionauth/typescript-client Refer to the FusionAuth API documentation to for request and response formats. * https://fusionauth.io/docs/v1/tech/apis/ * https://fusionauth.io/docs/v1/tech/client-libraries/typescript + +## Development + +* Set up a fusionauth instance. (Not sure exactly how to configure, TBD.) +* `sb test` diff --git a/package-lock.json b/package-lock.json index d292c2d..3eda13e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@fusionauth/typescript-client", - "version": "1.17.0", + "version": "1.17.3", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index f61b3bc..e75472e 100644 --- a/package.json +++ b/package.json @@ -18,10 +18,10 @@ "dist/fusionauth-typescript-client.*" ], "scripts": { - "test": "npx mocha build/test/FusionAuthClientTest.js && npx karma start", + "test": "npx mocha build/test/*.js && npx karma start", "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", "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", - "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", + "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", "prepare": "npx tsc && npm run build-browser && npm run build-browser-min" }, "repository": { diff --git a/src/DefaultRESTClient.ts b/src/DefaultRESTClient.ts index b24e3fc..3c11b12 100644 --- a/src/DefaultRESTClient.ts +++ b/src/DefaultRESTClient.ts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019, FusionAuth, All Rights Reserved + * Copyright (c) 2020, FusionAuth, All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -82,6 +82,15 @@ export default class DefaultRESTClient implements IRESTClient * @param body The object to be written to the request body as form data. */ withFormData(body: URLSearchParams): DefaultRESTClient { + const body2 = new URLSearchParams(); + if (body) { + body.forEach((value, name, searchParams) => { + if (value && value.length > 0 && value != "null" && value != "undefined") { + body2.set(name,value); + } + }); + body = body2; + } this.body = body; this.withHeader('Content-Type', 'application/x-www-form-urlencoded'); return this; diff --git a/test/DefaultRESTClientTest.ts b/test/DefaultRESTClientTest.ts new file mode 100644 index 0000000..7eeec71 --- /dev/null +++ b/test/DefaultRESTClientTest.ts @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2020, FusionAuth, All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific + * language governing permissions and limitations under the License. + */ + +'use strict'; + +import * as chai from 'chai'; +import DefaultRESTClient from "../src/DefaultRESTClient" +import {URLSearchParams} from "url"; + + +describe('#DefaultRESTClient()', function () { + + it('Can Create DefaultRESTClient', async () => { + const client = new DefaultRESTClient('http://localhost:9011'); + chai.assert.isNotNull(client); + }); + + describe('withFormData', function () { + it('null', async () => { + const client = new DefaultRESTClient('http://localhost:9011'); + let body = client.withFormData(null).body; + chai.assert.isNull(body); + }); + + it('empty', async () => { + const client = new DefaultRESTClient('http://localhost:9011'); + let params = new URLSearchParams(); + let body = client.withFormData(params).body; + chai.assert.isNotNull(body); + chai.assert.strictEqual(body.toString(), ""); + }); + + it('with one value', async () => { + const client = new DefaultRESTClient('http://localhost:9011'); + let params = new URLSearchParams(); + params.set('key','value'); + let body = client.withFormData(params).body; + chai.assert.isNotNull(body); + chai.assert.strictEqual(body.toString(), "key=value"); + }); + + it('with two values', async () => { + const client = new DefaultRESTClient('http://localhost:9011'); + let params = new URLSearchParams(); + params.set('key','value'); + params.set('key2','value2'); + let body = client.withFormData(params).body; + chai.assert.isNotNull(body); + chai.assert.strictEqual(body.toString(), "key=value&key2=value2"); + }); + + it('skips undefined value', async () => { + const client = new DefaultRESTClient('http://localhost:9011'); + let params = new URLSearchParams(); + params.set('key','value'); + params.set('key2',undefined); + let body = client.withFormData(params).body; + chai.assert.isNotNull(body); + chai.assert.strictEqual(body.toString(), "key=value"); + }); + + it('skips null value', async () => { + const client = new DefaultRESTClient('http://localhost:9011'); + let params = new URLSearchParams(); + params.set('key','value'); + params.set('key2',null); + let body = client.withFormData(params).body; + chai.assert.isNotNull(body); + chai.assert.strictEqual(body.toString(), "key=value"); + }); + + it('sets content type', async () => { + const client = new DefaultRESTClient('http://localhost:9011'); + let params = new URLSearchParams(); + let headers = client.withFormData(params).headers + chai.assert.isNotNull(headers); + chai.assert.strictEqual(headers['Content-Type'], "application/x-www-form-urlencoded"); + }); + + }); + +}); diff --git a/test/tsconfig.json b/test/tsconfig.json index 9dae5cb..173de2f 100644 --- a/test/tsconfig.json +++ b/test/tsconfig.json @@ -1,7 +1,8 @@ { "extends": "../tsconfig.json", "files": [ - "FusionAuthClientTest.ts" + "FusionAuthClientTest.ts", + "DefaultRESTClientTest.ts" ], "compilerOptions": { "types": [ @@ -11,4 +12,4 @@ "chai" ] } -} \ No newline at end of file +}