Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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).

Expand All @@ -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`
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
11 changes: 10 additions & 1 deletion src/DefaultRESTClient.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -82,6 +82,15 @@ export default class DefaultRESTClient<RT, ERT> implements IRESTClient<RT, ERT>
* @param body The object to be written to the request body as form data.
*/
withFormData(body: URLSearchParams): DefaultRESTClient<RT, ERT> {
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;
Expand Down
95 changes: 95 additions & 0 deletions test/DefaultRESTClientTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright (c) 2020, FusionAuth, All Rights Reserved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We normally preserve the first year, and append to it. 2019-2020 for example. No need to change necessary, but for future reference.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, thanks!

*
* 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");
});

});

});
5 changes: 3 additions & 2 deletions test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": "../tsconfig.json",
"files": [
"FusionAuthClientTest.ts"
"FusionAuthClientTest.ts",
"DefaultRESTClientTest.ts"
],
"compilerOptions": {
"types": [
Expand All @@ -11,4 +12,4 @@
"chai"
]
}
}
}