Skip to content

Commit d7ececd

Browse files
committed
Add several examples to the typescript client
1 parent ea9dbf2 commit d7ececd

22 files changed

+254
-5
lines changed

examples/browser-example/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FusionAuth Client Browser Example
2+
====
3+
4+
This directly contains the minimum layout for a browser project using FusionAuth.
5+
6+
You will need the client downloaded from the [releases page](https://github.com/FusionAuth/fusionauth-typescript-client/releases) as well as a script tag on your website that imports the client.
7+
8+
The client is namespaced under `FusionAuth` to prevent polluting the global namespace.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="WEB_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$" />
6+
<orderEntry type="sourceFolder" forTests="false" />
7+
</component>
8+
</module>

examples/browser-example/example.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
const client = new FusionAuth.FusionAuthClient('bf69486b-4733-4470-a592-f1bfce7af580', 'https://local.fusionauth.io');
18+
19+
client.retrieveUserByEmail('[email protected]')
20+
.then(clientResponse => {
21+
console.log("User:", JSON.stringify(clientResponse.response.user, null, 2));
22+
}).catch(console.error);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../dist/fusionauth-typescript-client.js
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../dist/fusionauth-typescript-client.min.js

examples/browser-example/fusionauth-typescript-client.min.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/browser-example/index.html

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<script src="fusionauth-typescript-client.min.js"></script>
5+
<script src="example.js"></script>
6+
</head>
7+
</html>
8+
<!--
9+
~ Copyright (c) 2020, FusionAuth, All Rights Reserved
10+
-->

examples/hybrid-example/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock.json

examples/hybrid-example/README.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
FusionAuth Client Hybrid Example
2+
====
3+
4+
This example exists as a minimal setup to get the FusionAuth Client working in both the browser and nodejs environment. For web publishing we chose to go with `browserify` for simplicity. This could also be done using webpack if preferred.
5+
6+
## Requirements
7+
8+
* `browserify` - Compiles stuff for the browser
9+
* `tsify` - Compiles typescript for browserify
10+
* `browserify-shim` - Allows code to work in both the browser and node by replacing require(x) with require(y)
11+
* `typescript` - tsify doesn't depend on typescript so that you can choose exactly which version you want
12+
* `tsconfig.json` - Provides the rules for `tsc` and `tsify` to compile typescript, we provided a minimal example.
13+
* `example.ts` - A script that actually uses FusionAuth client
14+
* `index.html` - A webpage that uses the script
15+
16+
## Building
17+
18+
To build nodejs you will need to use `tsc`. This compiles the project to `build/example.js` and produces a sourcemap. You can then run this in nodejs.
19+
20+
To build for the browser we use `npx browserify example.ts --debug -p tsify -t browserify-shim -o dist/example-browser.js` but we also add this line of code to package.json so that you can instead just call `npm run build-browser`. Both of these commands outputs to `dist/example-browser.js`. This example currently uses a browserify-shim for the client itself to replace the node version with the browser version we ship. This also means that the client must be added to the index.html as a `<script>` as well. In the future you will be able to omit this, and your code will be merged with the client code into one monolithic script.

examples/hybrid-example/example.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
import FusionAuthClient from "@fusionauth/typescript-client";
18+
19+
const client = new FusionAuthClient('bf69486b-4733-4470-a592-f1bfce7af580', 'https://local.fusionauth.io');
20+
21+
client.retrieveUserByEmail('[email protected]')
22+
.then(clientResponse => {
23+
console.log("User:", JSON.stringify(clientResponse.response.user, null, 2));
24+
}).catch(console.error);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="WEB_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$/../examples/hybrid-example" />
6+
<orderEntry type="sourceFolder" forTests="false" />
7+
</component>
8+
</module>

examples/hybrid-example/index.html

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
<!DOCTYPE html>
17+
<html>
18+
<head>
19+
<script src="node_modules/@fusionauth/typescript-client/dist/fusionauth-typescript-client.min.js"></script>
20+
<script src="dist/example-browser.js"></script>
21+
</head>
22+
</html>

examples/hybrid-example/package.json

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "hybrid-example",
3+
"private": true,
4+
"version": "1.0.0",
5+
"description": "",
6+
"main": "index.js",
7+
"scripts": {
8+
"test": "echo \"Error: no test specified\" && exit 1",
9+
"build-browser": "npx browserify example.ts --debug -p tsify -t browserify-shim -o dist/example-browser.js"
10+
},
11+
"keywords": [],
12+
"author": "Tyler Scott",
13+
"license": "Apache2.0",
14+
"browserify-shim": {
15+
"@fusionauth/typescript-client": "global:FusionAuth"
16+
},
17+
"dependencies": {
18+
"@fusionauth/typescript-client": "^1.16.0-RC.1"
19+
},
20+
"devDependencies": {
21+
"browserify": "^16.5.1",
22+
"browserify-shim": "^3.8.14",
23+
"tsify": "^4.0.1",
24+
"typescript": "^3.8.3"
25+
}
26+
}

examples/hybrid-example/tsconfig.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"compilerOptions": {
3+
"target": "esnext",
4+
"module": "commonjs",
5+
"sourceMap": true,
6+
"lib": [
7+
"esnext"
8+
],
9+
"outDir": "build",
10+
"types": [
11+
"node"
12+
]
13+
},
14+
"exclude": [
15+
"node_modules",
16+
"build"
17+
]
18+
}

examples/node-example/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock.json

examples/node-example/README.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FusionAuth Client NodeJS Example
2+
====
3+
4+
This example exists as a minimal setup to get the FusionAuth Client working in the nodejs environment.
5+
6+
## Requirements
7+
8+
* `typescript` - We use typescript in this example for type completion while we code
9+
* `tsconfig.json` - Provides the rules for `tsc` to compile typescript, we provided a minimal example
10+
* `example.ts` - A script that actually uses FusionAuth client
11+
12+
## Building
13+
14+
To build nodejs you will need to use `tsc`. This compiles the project to `build/example.js` and produces a sourcemap. You can then run this in nodejs.

examples/node-example/example.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
import FusionAuthClient from "@fusionauth/typescript-client";
18+
19+
const client = new FusionAuthClient('bf69486b-4733-4470-a592-f1bfce7af580', 'https://local.fusionauth.io');
20+
21+
client.retrieveUserByEmail('[email protected]')
22+
.then(clientResponse => {
23+
console.log("User:", JSON.stringify(clientResponse.response.user, null, 2));
24+
}).catch(console.error);
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="WEB_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$/../examples/node-example" />
6+
<orderEntry type="sourceFolder" forTests="false" />
7+
</component>
8+
</module>

examples/node-example/package.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "node-example",
3+
"private": true,
4+
"version": "1.0.0",
5+
"description": "",
6+
"main": "index.js",
7+
"scripts": {
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"keywords": [],
11+
"author": "Tyler Scott",
12+
"license": "Apache-2.0",
13+
"dependencies": {
14+
"@fusionauth/typescript-client": "^1.16.0-RC.1"
15+
}
16+
}

examples/node-example/tsconfig.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"compilerOptions": {
3+
"target": "esnext",
4+
"module": "commonjs",
5+
"sourceMap": true,
6+
"lib": [
7+
"esnext"
8+
],
9+
"outDir": "build",
10+
"types": [
11+
"node"
12+
]
13+
},
14+
"exclude": [
15+
"node_modules",
16+
"build"
17+
]
18+
}

fusionauth-typescript-client.iml

+1-4
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@
33
<component name="NewModuleRootManager" inherit-compiler-output="true">
44
<exclude-output />
55
<content url="file://$MODULE_DIR$">
6-
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
76
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
8-
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
97
<excludeFolder url="file://$MODULE_DIR$/build" />
108
<excludeFolder url="file://$MODULE_DIR$/dist" />
119
</content>
1210
<orderEntry type="inheritedJdk" />
1311
<orderEntry type="sourceFolder" forTests="false" />
1412
</component>
15-
</module>
16-
13+
</module>

tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"node_modules",
1818
"build",
1919
"dist",
20-
"test"
20+
"test",
21+
"examples"
2122
]
2223
}

0 commit comments

Comments
 (0)