Skip to content

Commit 333cf8c

Browse files
committed
feat(*): creating a JS SDK
0 parents  commit 333cf8c

File tree

12 files changed

+3996
-0
lines changed

12 files changed

+3996
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/node_modules

.npmrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
tag-version-prefix=""
2+
message="chore(release): bump to version %s :tada:"

README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Moneytree Link JavaScript SDK
2+
3+
This library will help you to simply integrate Moneytree tools such as My Account and the Vault without having to do it yourself.
4+
5+
6+
## Installation
7+
8+
### Browser based
9+
Include the script tag
10+
```html
11+
<script type="text/javascript" src="https://cdn.rawgit.com/moneytree/mt-link-javascript-sdk/<version>/dist/index.js"></script>
12+
```
13+
Change the `<version>` by the one you need (most likely the latest available).
14+
You can also replace `<version>` by `master` to always get the latest published, at your own risk.
15+
16+
### CommonJS
17+
Using NPM simply install the library
18+
```shell
19+
npm install mt-link-javascript-sdk
20+
```
21+
22+
Then you can use it directly in your code
23+
```js
24+
var mtLinkSdk = require('mt-link-javascript-sdk'); // es5
25+
// or
26+
import mtLinkSdk from 'mt-link-javascript-sdk'; // es-next
27+
```
28+
29+
## API
30+
31+
### Inititalising the API
32+
Call the following method with your desired configuration.
33+
`init(<config>)`
34+
35+
Config properties:
36+
```js
37+
{
38+
clientId, // string; // The id of the application that asks for authorization.
39+
response_type, // string; // Tells the authorization server which grant to execute.
40+
scope, // string[]; // A list of permissions that the application requires.
41+
redirectUri, // string; // Holds a URL. A successful response from this endpoint results in a redirect to this URL.
42+
locale, // string; // [optional] To force the display to a specific language (e.g.: en-AU)
43+
state, // string; // [optional] An opaque value, used for security purposes. If this request parameter is set in the request, then it is returned to the application as part of the redirect_uri.
44+
appToken, // string; // [optional] The Access Token granted through oauth
45+
isTestEnvironment // boolean; // [optional] If you wanna use the staging or production environemnt
46+
}
47+
```
48+
49+
### Open the page for the user to authorize your application
50+
`authorize()`
51+
52+
### Set the access token after init
53+
`setToken()`
54+
55+
### Open the setting page of the user account
56+
`openSettings()`
57+
58+
### Open the vault to let the user add credentials
59+
`openVault()`

dist/endpoints.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export declare const DOMAIN = "getmoneytree.com";
2+
export declare const VAULT: {
3+
SUBDOMAIN: string;
4+
TEST_SUBDOMAIN: string;
5+
};
6+
export declare const MY_ACCOUNT: {
7+
SUBDOMAIN: string;
8+
TEST_SUBDOMAIN: string;
9+
PATHS: {
10+
OAUTH: string;
11+
SETTINGS: string;
12+
};
13+
};

dist/index.d.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
interface Config {
2+
clientId: string;
3+
isTestEnvironment?: boolean;
4+
scope: string[];
5+
redirectUri?: string;
6+
responseType?: string;
7+
appToken?: string;
8+
locale?: string;
9+
state?: string;
10+
}
11+
declare class LinkSDK {
12+
private domains;
13+
private params;
14+
init(config: Config): void;
15+
setToken(appToken: string): void;
16+
authorize(newTab?: boolean): void;
17+
openVault(newTab?: boolean): void;
18+
openSettings(newTab?: boolean): void;
19+
}
20+
declare const _default: LinkSDK;
21+
export default _default;

dist/index.js

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

package.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "@moneytree/mt-link-javascript-sdk",
3+
"version": "1.0.0",
4+
"description": "Moneytree Link JavaScript SDK",
5+
"main": "dist/index.js",
6+
"repository": "https://github.com/moneytree/mt-link-javascript-sdk",
7+
"author": "Moneytree",
8+
"license": "MIT",
9+
"scripts": {
10+
"build": "rm -rf dist && webpack",
11+
"preversion": "npm run build && git add dist",
12+
"version": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0 && git add CHANGELOG.md",
13+
"precommit": "echo done",
14+
"commitmsg": "commitlint -e $GIT_PARAMS"
15+
},
16+
"dependencies": {
17+
"conventional-changelog-cli": "^2.0.1",
18+
"qs": "^6.5.2"
19+
},
20+
"devDependencies": {
21+
"@commitlint/cli": "^7.0.0",
22+
"@commitlint/config-conventional": "^7.0.1",
23+
"@types/qs": "^6.5.1",
24+
"awesome-typescript-loader": "^5.2.0",
25+
"husky": "^0.14.3",
26+
"typescript": "^3.0.1",
27+
"webpack": "^4.16.3",
28+
"webpack-cli": "^3.1.0"
29+
}
30+
}

src/endpoints.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export const DOMAIN = 'getmoneytree.com'
2+
3+
export const VAULT = {
4+
SUBDOMAIN: 'vault',
5+
TEST_SUBDOMAIN: 'vault-staging',
6+
};
7+
8+
export const MY_ACCOUNT = {
9+
SUBDOMAIN: 'myaccount',
10+
TEST_SUBDOMAIN: 'myaccount-staging',
11+
PATHS: {
12+
OAUTH: 'oauth/authorize',
13+
SETTINGS: 'settings'
14+
}
15+
}

src/index.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import * as qs from 'qs';
2+
import { DOMAIN, MY_ACCOUNT, VAULT } from './endpoints';
3+
4+
interface Config {
5+
clientId: string;
6+
isTestEnvironment?: boolean;
7+
scope: string[];
8+
redirectUri?: string;
9+
responseType?: string;
10+
appToken?: string;
11+
locale?: string;
12+
state?: string;
13+
}
14+
15+
interface Params {
16+
client_id: string;
17+
redirect_uri: string;
18+
response_type: string;
19+
scope: string;
20+
locale?: string;
21+
access_token?: string;
22+
state?: string;
23+
}
24+
25+
class LinkSDK {
26+
private domains: { [name: string]: string }
27+
private params: Params
28+
29+
init(config: Config): void {
30+
if (!config.clientId) {
31+
throw new Error('Need a clientId to initialise');
32+
}
33+
34+
this.params = {
35+
client_id:config. clientId,
36+
redirect_uri: config.redirectUri || `${location.protocol}//${location.host}/callback`,
37+
response_type: config.responseType || 'token',
38+
scope: config.scope.join(' '),
39+
access_token: config.appToken,
40+
locale: config.locale,
41+
state: config.state
42+
};
43+
44+
const subdomain = config.isTestEnvironment ? 'TEST_SUBDOMAIN' : 'SUBDOMAIN';
45+
this.domains = {
46+
vault: `${VAULT[subdomain]}.${DOMAIN}`,
47+
myaccount: `${MY_ACCOUNT[subdomain]}.${DOMAIN}`
48+
};
49+
}
50+
51+
// Set the token from callback or server
52+
setToken(appToken: string): void {
53+
this.params.access_token = appToken;
54+
}
55+
56+
// Open My Account to authorize application to use MtLink API
57+
authorize(newTab: boolean = false): void {
58+
const { PATHS: { OAUTH }} = MY_ACCOUNT;
59+
const params = qs.stringify(this.params);
60+
window.open(`https://${this.domains.myaccount}/${OAUTH}?${params}`, newTab ? '_blank' : '_self');
61+
}
62+
63+
// Open the Vault page
64+
openVault(newTab: boolean = false): void {
65+
const params = qs.stringify(this.params);
66+
window.open(`https://${this.domains.vault}?${params}`, newTab ? '_blank' : '_self');
67+
}
68+
69+
// Open the Guest settings page
70+
openSettings(newTab: boolean = false): void {
71+
const { PATHS: { SETTINGS }} = MY_ACCOUNT;
72+
const params = qs.stringify(this.params);
73+
window.open(`https://${this.domains.myaccount}?${params}/${SETTINGS}`, newTab ? '_blank' : '_self');
74+
}
75+
}
76+
77+
export default new LinkSDK();

tsconfig.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"module": "commonjs",
5+
"declaration": true,
6+
"outDir": "./dist",
7+
"strict": true,
8+
"strictPropertyInitialization": false,
9+
"noImplicitAny": true,
10+
"strictNullChecks": true,
11+
"lib": [ "es2015", "dom" ]
12+
},
13+
"include": [
14+
"src/**/*"
15+
]
16+
}

webpack.config.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const path = require('path');
2+
3+
module.exports = {
4+
mode: 'production',
5+
entry: './src/index.ts',
6+
output: {
7+
path: path.resolve(__dirname, 'dist'),
8+
filename: 'index.js',
9+
libraryTarget: 'umd',
10+
library: 'mtLinkSdk',
11+
libraryExport: 'default',
12+
umdNamedDefine: true
13+
},
14+
resolve: {
15+
extensions: ['.ts', '.tsx', '.js']
16+
},
17+
module: {
18+
rules: [
19+
{
20+
test: /\.tsx?$/,
21+
loader: 'awesome-typescript-loader'
22+
}
23+
]
24+
}
25+
};

0 commit comments

Comments
 (0)