From e994b37b9db93a2790841f9de4be2c5fb61b5e53 Mon Sep 17 00:00:00 2001 From: Dan Moore Date: Thu, 25 Jun 2020 12:23:18 -0600 Subject: [PATCH 1/8] addl info about usefulness of lib. --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 520aa4d..715d6dd 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,5 @@ 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 + + From 2a32913a9d59a7f8a9f02d182d66298c85c03a3e Mon Sep 17 00:00:00 2001 From: Dan Moore Date: Thu, 25 Jun 2020 15:23:30 -0600 Subject: [PATCH 2/8] Added test for DefaultRESTClient. --- test/DefaultRESTClientTest.ts | 95 +++++++++++++++++++++++++++++++++++ test/tsconfig.json | 5 +- 2 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 test/DefaultRESTClientTest.ts diff --git a/test/DefaultRESTClientTest.ts b/test/DefaultRESTClientTest.ts new file mode 100644 index 0000000..b92d68e --- /dev/null +++ b/test/DefaultRESTClientTest.ts @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2019, 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 +} From f5d9cda7ed066a0153fb0f9d9ab00921e5edded3 Mon Sep 17 00:00:00 2001 From: Dan Moore Date: Thu, 25 Jun 2020 15:23:42 -0600 Subject: [PATCH 3/8] Added test for DefaultRESTClient. --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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": { From 0b727a33c57886783eefb154588cbee99a36e231 Mon Sep 17 00:00:00 2001 From: Dan Moore Date: Thu, 25 Jun 2020 15:23:56 -0600 Subject: [PATCH 4/8] Version bump. --- package-lock.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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": { From 97762191e309bf86a25fcf516ffeecaeca185149 Mon Sep 17 00:00:00 2001 From: Dan Moore Date: Thu, 25 Jun 2020 15:24:16 -0600 Subject: [PATCH 5/8] Stubbed out development section. --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 715d6dd..d8f1aff 100644 --- a/README.md +++ b/README.md @@ -19,4 +19,7 @@ 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` From d61734aa4ad0d0276f9d6d3f5f28470d3628aa23 Mon Sep 17 00:00:00 2001 From: Dan Moore Date: Thu, 25 Jun 2020 15:25:26 -0600 Subject: [PATCH 6/8] Don't pass any null or undefined values to the form. We have to do it this way because URLSearchParams, which is what we get, converts everything to strings. That's why we do the string comparisons, rather than comparing with null or undefined or using typeof. --- src/DefaultRESTClient.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/DefaultRESTClient.ts b/src/DefaultRESTClient.ts index b24e3fc..66ae1e2 100644 --- a/src/DefaultRESTClient.ts +++ b/src/DefaultRESTClient.ts @@ -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; From ad94fc1e04c3bb1a6320e3c5b4f65d7cfec3efd8 Mon Sep 17 00:00:00 2001 From: Dan Moore Date: Wed, 1 Jul 2020 15:31:04 -0600 Subject: [PATCH 7/8] Updated copyright date --- src/DefaultRESTClient.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DefaultRESTClient.ts b/src/DefaultRESTClient.ts index 66ae1e2..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. From 667870234d5357c0cd6e7bee7b55376d176bf079 Mon Sep 17 00:00:00 2001 From: Dan Moore Date: Wed, 1 Jul 2020 15:31:29 -0600 Subject: [PATCH 8/8] Updated copyright date --- test/DefaultRESTClientTest.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/DefaultRESTClientTest.ts b/test/DefaultRESTClientTest.ts index b92d68e..7eeec71 100644 --- a/test/DefaultRESTClientTest.ts +++ b/test/DefaultRESTClientTest.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.