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
45 changes: 12 additions & 33 deletions resources/application.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import axios, { AxiosResponse } from 'axios'
import { Application, ApplicationDocument, CreateApplicationRequest } from '../types/application';
import { UnitResponse, Include, UnitError } from '../types/core';
import { BaseResource } from './baseResource';

export class Applications {
private token: string;
private basePath = 'https://api.s.unit.sh';
private resourcePath = '/applications'
export class Applications extends BaseResource{

constructor(token: string) {
this.token = token
constructor(token: string, basePath: string) {
super(token, basePath + '/applications');
}

public async list(params: ApplicationListParams): Promise<UnitResponse<Application[]> | UnitError> {
var headers = {
'Authorization': `Bearer ${this.token}`
};

var parameters = {
'page[limit]': (params.limit ? params.limit : 100),
'page[offset]': (params.offset ? params.offset : 0),
Expand All @@ -24,39 +18,24 @@ export class Applications {
...(params.tags && { 'filter[tags]': params.tags }),
'sort': params.sort ? params.sort : '-createdAt'
}
var res = await axios.get<AxiosResponse<Application[] | UnitError>>(`${this.basePath + this.resourcePath}`, { headers: headers, params: parameters })
.then(r => r.data)
.catch(error => { return error.response.data })

return res

return this.httpGet<UnitResponse<Application[]>>('', { params: parameters })
}

public async create(request: CreateApplicationRequest): Promise<UnitResponse<Application> | UnitError> {
var headers = {
'Authorization': `Bearer ${this.token}`,
'Content-Type': 'application/vnd.api+json'
};

var res = await axios.post<UnitResponse<Application> | UnitError>(`${this.basePath + this.resourcePath}`, { data: request }, { headers })
.then(r => r.data)
.catch(error => { return error.response.data })

return res
return this.httpPost<UnitResponse<Application>>('', { data: request }, { headers })
}

public async get(id: number): Promise<UnitResponse<Application> & Include<ApplicationDocument[]> | UnitError> {
var headers = {
'Authorization': `Bearer ${this.token}`
};

var path = `${this.basePath + this.resourcePath}/${id}`

var res = await axios.get<UnitResponse<Application> & Include<ApplicationDocument[]> | UnitError>(path, { headers })
.then(r => r.data)
.catch(error => { return error.response.data })
public async get(applicationId: number): Promise<UnitResponse<Application> & Include<ApplicationDocument[]> | UnitError> {
return this.httpGet<UnitResponse<Application> & Include<ApplicationDocument[]>>(`/${applicationId}`)
}

return res
public async listDocuments(applicationId: number): Promise<UnitResponse<ApplicationDocument[]> | UnitError> {
return this.httpGet<UnitResponse<ApplicationDocument[]>>(`/${applicationId}/documents`)
}
}

Expand Down Expand Up @@ -96,4 +75,4 @@ interface ApplicationListParams {
* default: sort=-createdAt
*/
sort?: string
}
}
56 changes: 56 additions & 0 deletions resources/baseResource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import axios from "axios"
import { UnitError } from "../types/core"

export class BaseResource {
private resourcePath: string
private headers: {};

constructor(token: string, resourcePath: string) {
this.resourcePath = resourcePath

this.headers = {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/vnd.api+json'
};
}

protected async httpGet<T>(path: string, config?: { headers?: object, params?: object }) : Promise<UnitError | T> {

var conf = {
headers: config?.headers ? { ...this.headers, ...config?.headers } : this.headers,
...(config?.params && { params: (config.params)})
}

return await axios.get<T | UnitError>(this.resourcePath + path, conf)
.then(r => r.data)
.catch(error => { return error.response.data })
}

protected async httpPatch<T>(path: string, data: object, config?: { headers?: object, params?: object }) : Promise<UnitError | T> {
var conf = {
headers: config?.headers ? { ...this.headers, ...config?.headers } : this.headers,
...(config?.params && { params: (config.params) })
}

return await axios.patch<T | UnitError>(this.resourcePath + path, data, conf)
.then(r => r.data)
.catch(error => { return error.response.data })
}

protected async httpPost<T>(path: string, data: object, config?: { headers?: object, params?: object }) : Promise<UnitError | T>{
var conf = {
headers: config?.headers ? { ...this.headers, ...config?.headers } : this.headers,
...(config?.params && { params: (config.params) })
}

return await axios.post<T | UnitError>(this.resourcePath + path, data, conf)
.then(r => r.data)
.catch(error => { return error.response.data })
}

protected async httpDelete<T>(path: string) : Promise<UnitError | T> {
return await axios.delete<T | UnitError>(this.resourcePath + path)
.then(r => r.data)
.catch(error => { return error.response.data })
}
}
75 changes: 75 additions & 0 deletions resources/customer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { UnitResponse, UnitError} from "../types/core";
import { Customer, PatchBusinessCustomerRequest, PatchIndividualCustomerRequest } from "../types/customer";
import { BaseResource } from "./baseResource";

export class Customers extends BaseResource {

constructor(token: string, basePath: string) {
super(token, basePath + '/customers')
}

public async updateIndividual(request: PatchIndividualCustomerRequest) {
return this.httpPatch<UnitResponse<Customer>>(`/${request.customerId}`, { data: request.data })
}

public async updateBusiness(request: PatchBusinessCustomerRequest) {
return this.httpPatch<UnitResponse<Customer>>(`/${request.customerId}`, { data: request.data })
}

public async get(customerId: number): Promise<UnitResponse<Customer> | UnitError> {

return this.httpGet<UnitResponse<Customer>>(`/${customerId}`)
}

public async list(params?: CustomersListParams): Promise<UnitResponse<Customer[]> | UnitError> {

var parameters = {
'page[limit]': (params?.limit ? params.limit : 100),
'page[offset]': (params?.offset ? params.offset : 0),
...(params?.query && { 'filter[query]': params.query }),
...(params?.email && { 'filter[email]': params.email }),
...(params?.tags && { 'filter[tags]': params.tags }),
'sort': params?.sort ? params.sort : '-createdAt'
}

return this.httpGet<UnitResponse<Customer[]>>('', { params: parameters })
}
}

interface CustomersListParams {
/**
* Maximum number of resources that will be returned. Maximum is 1000 resources. See Pagination.
* default: 100
*/
limit?: number,

/**
* Number of resources to skip. See Pagination.
* default: 0
*/
offset?: number,

/**
* Optional. Search term according to the Full-Text Search Rules.
* default: empty
*/
query?: string,

/**
* Optional. Filter customers by email address (case sensitive).
* default: empty
*/
email?: string,

/**
* Optional. Filter customers by Tags.
* default: empty
*/
tags?: Object,

/**
* Optional. sort=createdAt for ascending order or sort=-createdAt (leading minus sign) for descending order.
* default: sort=-createdAt
*/
sort?: string
}
9 changes: 5 additions & 4 deletions resources/unit.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Applications } from './application'
import { Customers } from './customer';

export class Unit {
private token: string;
public applications: Applications
public customers: Customers

constructor(token: string) {
this.token = token;
this.applications = new Applications(token);
constructor(token: string, basePath: string) {
this.applications = new Applications(token, basePath);
this.customers = new Customers(token, basePath)
}
}

23 changes: 10 additions & 13 deletions types/application.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Address, BeneficialOwner, BusinessContact, FullName, Officer, Phone, State } from "./core"
import { Address, BeneficialOwner, BusinessContact, FullName, Officer, Phone, State, Relationship } from "./core"

export type ApplicationStatus =
"AwaitingDocuments" | //Certain documents are required for the process to continue. You may upload them via Upload Document.
Expand All @@ -7,11 +7,6 @@ export type ApplicationStatus =
"Approved" | //The application was approved. A Customer resource was created.
"Denied" //The application was denied. A Customer resource will not be created.

/**
* More about [Relationship](https://developers.unit.co/#relationships)
*/
export type Relationship = null | [] | { type: string, id: string } | Array<{ type: string, id: string }>

export interface Application {
/**
* Identifier of the application resource.
Expand All @@ -21,7 +16,7 @@ export interface Application {
/**
* Type of the application resource.
*/
type: string
type: 'IndividualApplication' | 'BusinessApplication'

/**
* The relationships object describes the relationship between the current resource and other resources.
Expand Down Expand Up @@ -131,6 +126,7 @@ export interface IndividualApplication extends Application {

export interface BusinessApplication extends Application {
type: 'BusinessApplication',

attributes: {
/**
* One of AwaitingDocuments, PendingReview, Approved, Pending, or Denied, see Application Statuses.
Expand Down Expand Up @@ -196,7 +192,7 @@ export interface BusinessApplication extends Application {
/**
* Array of beneficial owners of the business. Beneficial Owner is anyone with more than 25% ownership. Beneficial Owners would need to go over KYC process and provide documents.
*/
beneficialOwners: Array<BeneficialOwner>
beneficialOwners: BeneficialOwner[]

/**
* See [Tags](https://developers.unit.co/#tags).
Expand Down Expand Up @@ -244,7 +240,7 @@ export interface ApplicationDocument {
type: "document"

/**
* representing the document’s data.
* Representing the document’s data.
*/
attributes: {
/**
Expand Down Expand Up @@ -307,11 +303,11 @@ export interface CreateApplicationRequest {

export interface CreateIndividualApplicationRequest extends CreateApplicationRequest {
type: 'individualApplication'
attributes:
{

attributes: {
/**
* SSN of the individual (numbers only). Either an SSN or a passport number is required.
*/
* SSN of the individual (numbers only). Either an SSN or a passport number is required.
*/
ssn?: string

/**
Expand Down Expand Up @@ -384,6 +380,7 @@ export interface CreateIndividualApplicationRequest extends CreateApplicationReq

export interface CreateBusinessApplicationRequest extends CreateApplicationRequest {
type: 'businessApplication'

attributes: {
/**
* Name of the business.
Expand Down
Loading