Skip to content

Commit

Permalink
feat(*): creating a JS SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
mt-micky committed Aug 1, 2018
0 parents commit 333cf8c
Show file tree
Hide file tree
Showing 12 changed files with 3,996 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules
2 changes: 2 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
tag-version-prefix=""
message="chore(release): bump to version %s :tada:"
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Moneytree Link JavaScript SDK

This library will help you to simply integrate Moneytree tools such as My Account and the Vault without having to do it yourself.


## Installation

### Browser based
Include the script tag
```html
<script type="text/javascript" src="https://cdn.rawgit.com/moneytree/mt-link-javascript-sdk/<version>/dist/index.js"></script>
```
Change the `<version>` by the one you need (most likely the latest available).
You can also replace `<version>` by `master` to always get the latest published, at your own risk.

### CommonJS
Using NPM simply install the library
```shell
npm install mt-link-javascript-sdk
```

Then you can use it directly in your code
```js
var mtLinkSdk = require('mt-link-javascript-sdk'); // es5
// or
import mtLinkSdk from 'mt-link-javascript-sdk'; // es-next
```

## API

### Inititalising the API
Call the following method with your desired configuration.
`init(<config>)`

Config properties:
```js
{
clientId, // string; // The id of the application that asks for authorization.
response_type, // string; // Tells the authorization server which grant to execute.
scope, // string[]; // A list of permissions that the application requires.
redirectUri, // string; // Holds a URL. A successful response from this endpoint results in a redirect to this URL.
locale, // string; // [optional] To force the display to a specific language (e.g.: en-AU)
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.
appToken, // string; // [optional] The Access Token granted through oauth
isTestEnvironment // boolean; // [optional] If you wanna use the staging or production environemnt
}
```

### Open the page for the user to authorize your application
`authorize()`

### Set the access token after init
`setToken()`

### Open the setting page of the user account
`openSettings()`

### Open the vault to let the user add credentials
`openVault()`
13 changes: 13 additions & 0 deletions dist/endpoints.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export declare const DOMAIN = "getmoneytree.com";
export declare const VAULT: {
SUBDOMAIN: string;
TEST_SUBDOMAIN: string;
};
export declare const MY_ACCOUNT: {
SUBDOMAIN: string;
TEST_SUBDOMAIN: string;
PATHS: {
OAUTH: string;
SETTINGS: string;
};
};
21 changes: 21 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
interface Config {
clientId: string;
isTestEnvironment?: boolean;
scope: string[];
redirectUri?: string;
responseType?: string;
appToken?: string;
locale?: string;
state?: string;
}
declare class LinkSDK {
private domains;
private params;
init(config: Config): void;
setToken(appToken: string): void;
authorize(newTab?: boolean): void;
openVault(newTab?: boolean): void;
openSettings(newTab?: boolean): void;
}
declare const _default: LinkSDK;
export default _default;
1 change: 1 addition & 0 deletions dist/index.js

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

30 changes: 30 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "@moneytree/mt-link-javascript-sdk",
"version": "1.0.0",
"description": "Moneytree Link JavaScript SDK",
"main": "dist/index.js",
"repository": "https://github.com/moneytree/mt-link-javascript-sdk",
"author": "Moneytree",
"license": "MIT",
"scripts": {
"build": "rm -rf dist && webpack",
"preversion": "npm run build && git add dist",
"version": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0 && git add CHANGELOG.md",
"precommit": "echo done",
"commitmsg": "commitlint -e $GIT_PARAMS"
},
"dependencies": {
"conventional-changelog-cli": "^2.0.1",
"qs": "^6.5.2"
},
"devDependencies": {
"@commitlint/cli": "^7.0.0",
"@commitlint/config-conventional": "^7.0.1",
"@types/qs": "^6.5.1",
"awesome-typescript-loader": "^5.2.0",
"husky": "^0.14.3",
"typescript": "^3.0.1",
"webpack": "^4.16.3",
"webpack-cli": "^3.1.0"
}
}
15 changes: 15 additions & 0 deletions src/endpoints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const DOMAIN = 'getmoneytree.com'

export const VAULT = {
SUBDOMAIN: 'vault',
TEST_SUBDOMAIN: 'vault-staging',
};

export const MY_ACCOUNT = {
SUBDOMAIN: 'myaccount',
TEST_SUBDOMAIN: 'myaccount-staging',
PATHS: {
OAUTH: 'oauth/authorize',
SETTINGS: 'settings'
}
}
77 changes: 77 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import * as qs from 'qs';
import { DOMAIN, MY_ACCOUNT, VAULT } from './endpoints';

interface Config {
clientId: string;
isTestEnvironment?: boolean;
scope: string[];
redirectUri?: string;
responseType?: string;
appToken?: string;
locale?: string;
state?: string;
}

interface Params {
client_id: string;
redirect_uri: string;
response_type: string;
scope: string;
locale?: string;
access_token?: string;
state?: string;
}

class LinkSDK {
private domains: { [name: string]: string }
private params: Params

init(config: Config): void {
if (!config.clientId) {
throw new Error('Need a clientId to initialise');
}

this.params = {
client_id:config. clientId,
redirect_uri: config.redirectUri || `${location.protocol}//${location.host}/callback`,
response_type: config.responseType || 'token',
scope: config.scope.join(' '),
access_token: config.appToken,
locale: config.locale,
state: config.state
};

const subdomain = config.isTestEnvironment ? 'TEST_SUBDOMAIN' : 'SUBDOMAIN';
this.domains = {
vault: `${VAULT[subdomain]}.${DOMAIN}`,
myaccount: `${MY_ACCOUNT[subdomain]}.${DOMAIN}`
};
}

// Set the token from callback or server
setToken(appToken: string): void {
this.params.access_token = appToken;
}

// Open My Account to authorize application to use MtLink API
authorize(newTab: boolean = false): void {
const { PATHS: { OAUTH }} = MY_ACCOUNT;
const params = qs.stringify(this.params);
window.open(`https://${this.domains.myaccount}/${OAUTH}?${params}`, newTab ? '_blank' : '_self');
}

// Open the Vault page
openVault(newTab: boolean = false): void {
const params = qs.stringify(this.params);
window.open(`https://${this.domains.vault}?${params}`, newTab ? '_blank' : '_self');
}

// Open the Guest settings page
openSettings(newTab: boolean = false): void {
const { PATHS: { SETTINGS }} = MY_ACCOUNT;
const params = qs.stringify(this.params);
window.open(`https://${this.domains.myaccount}?${params}/${SETTINGS}`, newTab ? '_blank' : '_self');
}
}

export default new LinkSDK();
16 changes: 16 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"declaration": true,
"outDir": "./dist",
"strict": true,
"strictPropertyInitialization": false,
"noImplicitAny": true,
"strictNullChecks": true,
"lib": [ "es2015", "dom" ]
},
"include": [
"src/**/*"
]
}
25 changes: 25 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const path = require('path');

module.exports = {
mode: 'production',
entry: './src/index.ts',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'index.js',
libraryTarget: 'umd',
library: 'mtLinkSdk',
libraryExport: 'default',
umdNamedDefine: true
},
resolve: {
extensions: ['.ts', '.tsx', '.js']
},
module: {
rules: [
{
test: /\.tsx?$/,
loader: 'awesome-typescript-loader'
}
]
}
};
Loading

0 comments on commit 333cf8c

Please sign in to comment.