-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #126 from starkbank/feature/request-types
Map request methods in Typescript
- Loading branch information
Showing
3 changed files
with
285 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
///<reference types="../types/" /> | ||
import starkbank from "starkbank"; | ||
import assert from 'assert'; | ||
const generateExampleInvoicesJson = require('./utils/invoice.js').generateExampleInvoicesJson; | ||
const random = require('./utils/random'); | ||
|
||
starkbank.user = require('./utils/user').exampleProject; | ||
|
||
describe('TestRequestGet', function(){ | ||
jest.setTimeout(10000); | ||
it('test_success', async () => { | ||
let path = "/invoice/"; | ||
let query = {"limit": 10, "status": "paid"}; | ||
let i=0; | ||
let list = await starkbank.request.get(path, query); | ||
console.log(list) | ||
for (let invoice of list["content"]["invoices"]) { | ||
assert(typeof invoice.id == 'string'); | ||
i += 1; | ||
} | ||
assert(i === 10); | ||
}); | ||
}); | ||
|
||
describe('TestRequestGetPdf', function(){ | ||
jest.setTimeout(10000); | ||
it('test_success', async () => { | ||
let path = "/invoice/"; | ||
let query = {"limit": 10, "status": "paid"}; | ||
let list = await starkbank.request.get(path, query); | ||
let pdf = await starkbank.request.get(`invoice/${list["content"]["invoices"][0]["id"]}/pdf`) | ||
assert(pdf["content"].length>=1000) | ||
}); | ||
}); | ||
|
||
describe('TestRequestGetQrcode', function(){ | ||
jest.setTimeout(10000); | ||
it('test_success', async () => { | ||
let path = "/invoice/"; | ||
let query={"limit": 10, "status": "paid"}; | ||
let list = await starkbank.request.get(path, query); | ||
let pdf = await starkbank.request.get(`invoice/${list["content"]["invoices"][0]["id"]}/qrcode`, {"size": 15}) | ||
assert(pdf["content"].length>=1000) | ||
}); | ||
}); | ||
|
||
describe('TestRequestGetReversalReceipt', function(){ | ||
jest.setTimeout(10000); | ||
it('test_success', async () => { | ||
let path = "/deposit/log"; | ||
let query={"limit": 1, "types": "reversed"} | ||
let list = await starkbank.request.get(path, query); | ||
let pdf = await starkbank.request.get(`deposit/log/${list["content"]["logs"][0]["id"]}/pdf`) | ||
assert(pdf["content"].length>=1000) | ||
}); | ||
}); | ||
|
||
describe('TestRequestGetPagination', function(){ | ||
jest.setTimeout(10000); | ||
it('test_success', async () => { | ||
const path = "/invoice/"; | ||
let cursor = null; | ||
let counter = 0; | ||
let request: any | ||
while (true) { | ||
request = await starkbank.request.get(path, {"cursor":cursor}); | ||
cursor = request["content"]["cursor"] | ||
for (let i of request["content"]["invoices"]) { | ||
counter+=1; | ||
} | ||
if (cursor == null || counter>199) { | ||
break; | ||
} | ||
} | ||
assert(counter<=200); | ||
}); | ||
}); | ||
|
||
describe('TestRequestPost', function(){ | ||
jest.setTimeout(10000); | ||
it('test_success', async () => { | ||
const path = "/invoice/"; | ||
const data = { | ||
"invoices": [{ | ||
"amount": 100, | ||
"name": "Iron Bank S.A.", | ||
"taxId": "20.018.183/0001-80" | ||
}] | ||
}; | ||
let request = await starkbank.request.post(path, data); | ||
assert(request["content"]["invoices"][0]["taxId"] == "20.018.183/0001-80") | ||
}); | ||
}); | ||
|
||
describe('TestRequestPatch', function(){ | ||
jest.setTimeout(10000); | ||
it('test_success', async () => { | ||
let request = await starkbank.request.get("/invoice/", {"limit": 1, "status": "paid"}); | ||
const amount = request["content"]["invoices"][0]["amount"] | ||
let a = await starkbank.request.patch( | ||
`/invoice/${request["content"]["invoices"][0]["id"]}`, | ||
{"amount": amount - amount}, | ||
) | ||
console.log(a) | ||
const finalState = await starkbank.request.get(`/invoice/${request["content"]["invoices"][0]["id"]}`) | ||
assert(finalState["content"]["invoice"]["amount"] == 0) | ||
}); | ||
}); | ||
|
||
describe('TestRequestPut', function(){ | ||
jest.setTimeout(10000); | ||
it('test_success', async () => { | ||
const data = { | ||
"profiles": [ | ||
{ | ||
"interval": "day", | ||
"delay": 0 | ||
} | ||
] | ||
}; | ||
await starkbank.request.put( | ||
"/split-profile/", | ||
data, | ||
) | ||
|
||
let result = await starkbank.request.get("/split-profile/") | ||
assert(result["content"]["profiles"][0]["delay"] == 0) | ||
}) | ||
}) | ||
|
||
describe('TestRequestDelete', function(){ | ||
jest.setTimeout(10000); | ||
it('test_success', async () => { | ||
let futureDate = new Date(); | ||
futureDate.setDate(futureDate.getDate() + 10); | ||
|
||
const data = { | ||
"transfers": [ | ||
{ | ||
"amount": 10000, | ||
"name": "Steve Rogers", | ||
"taxId": "330.731.970-10", | ||
"bankCode": "001", | ||
"branchCode": "1234", | ||
"accountNumber": "123456-0", | ||
"accountType": "checking", | ||
"scheduled": futureDate.toISOString().split("T")[0], | ||
"externalId": new Date().getTime().toString(), | ||
} | ||
] | ||
} | ||
|
||
let create = await starkbank.request.post( | ||
"/transfer/", | ||
data, | ||
) | ||
|
||
await starkbank.request.delete( | ||
`/transfer/${create["content"]["transfers"][0]["id"]}`, | ||
) | ||
let finalStatus = await starkbank.request.get( | ||
`/transfer/${create["content"]["transfers"][0]["id"]}`, | ||
) | ||
|
||
assert(finalStatus["content"]["transfer"]["status"] == 'canceled') | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
|
||
declare module 'starkbank' { | ||
|
||
export namespace request { | ||
|
||
/** | ||
* | ||
* create any StarkBank resource | ||
* | ||
* @description Receive a json of resources previously created in StarkBank's API | ||
* | ||
* Parameters (required): | ||
* @param path [string]: StarkBank resource's route. ex: "/invoice/" | ||
* @param body [object]: request parameters. ex: {"invoices": [{"amount": 100, "name": "Iron Bank S.A.", "taxId": "20.018.183/0001-80"}]} | ||
* | ||
* Parameters (optional): | ||
* @param user [Organization/Project object, default null]: Organization or Project object. Not necessary if starkbank.user was set before function call | ||
* @param query [object, default None]: Query parameters. ex: {"expand": ["securityCode", "number", "rules"]} | ||
* | ||
* Return: | ||
* @returns a list of StarkBank objects with updated attributes | ||
* | ||
*/ | ||
export function post(path: String, body:{[key: string]: any}, query?: {}, user?: Project | Organization | null): Promise<{status: number, content: any, headers: {}}>; | ||
|
||
/** | ||
* | ||
* Retrieve any StarkBank resource | ||
* | ||
* @description Receive a json of resources previously created in StarkBank's API | ||
* | ||
* Parameters (required): | ||
* @param path [string]: StarkBank resource's route. ex: "/invoice/" | ||
* | ||
* Parameters (optional): | ||
* @param user [Organization/Project object, default null]: Organization or Project object. Not necessary if starkbank.user was set before function call | ||
* @param query [object, default None]: Query parameters. ex: {"limit": 1, "status": "paid"} | ||
* | ||
* Return: | ||
* @returns a list of StarkBank objects with updated attributes | ||
* | ||
*/ | ||
export function get( | ||
path: string, | ||
query?: { | ||
cursor?: string | null, | ||
limit?: number | null, | ||
after?: string | null, | ||
before?: string | null, | ||
status?: string | null, | ||
tags?: string[] | null, | ||
ids?: string[] | null, | ||
[key: string]: any; | ||
}, | ||
user?: Project | Organization | null | ||
): Promise<{status: number, content: any, headers: {}}> | ||
|
||
/** | ||
* | ||
* Update any StarkBank resource | ||
* | ||
* @description Receive a json of resources previously created in StarkBank's API | ||
* | ||
* Parameters (required): | ||
* @param path [string]: StarkBank resource's route. ex: "/invoice/" | ||
* @param body [object]: request parameters. ex: {"invoices": [{"amount": 100, "name": "Iron Bank S.A.", "taxId": "20.018.183/0001-80"}]} | ||
* | ||
* Parameters (optional): | ||
* @param user [Organization/Project object, default null]: Organization or Project object. Not necessary if starkbank.user was set before function call | ||
* | ||
* Return: | ||
* @returns a list of StarkBank objects with updated attributes | ||
* | ||
*/ | ||
export function patch(path: string, body:{[key: string]: any}, user?: Project | Organization | null): Promise<{status: number, content: any, headers: {}}>; | ||
|
||
/** | ||
* | ||
* Put any StarkBank resource | ||
* | ||
* @description Receive a json of resources previously created in StarkBank's API | ||
* | ||
* Parameters (required): | ||
* @param path [string]: StarkBank resource's route. ex: "/invoice/" | ||
* @param body [object]: request parameters. ex: {"invoices": [{"amount": 100, "name": "Iron Bank S.A.", "taxId": "20.018.183/0001-80"}]} | ||
* | ||
* Parameters (optional): | ||
* @param user [Organization/Project object, default null]: Organization or Project object. Not necessary if starkbank.user was set before function call | ||
* | ||
* Return: | ||
* @returns a list of StarkBank objects with updated attributes | ||
* | ||
*/ | ||
export function put(path: string, body:{[key: string]: any}, user?: Project | Organization | null): Promise<{status: number, content: any, headers: {}}>; | ||
|
||
/** | ||
* | ||
* Delete any StarkBank resource | ||
* | ||
* @description Delete a single resource previously created in StarkBank's API | ||
* | ||
* Parameters (required): | ||
* @param path [string]: StarkBank resource's r\oute. ex: "/invoice/" | ||
* | ||
* Parameters (optional): | ||
* @param user [Organization/Project object, default null]: Organization or Project object. Not necessary if starkbank.user was set before function call | ||
* @param body [object]: request parameters. ex: {"invoices": [{"amount": 100, "name": "Iron Bank S.A.", "taxId": "20.018.183/0001-80"}]} | ||
* | ||
* Return: | ||
* @returns a list of StarkBank objects with updated attributes | ||
* | ||
*/ | ||
function _delete(path: string, body?:{[key: string]: any}, user?: Project | Organization | null): Promise<{status: number, content: any, headers: {}}>; | ||
export { _delete as delete } | ||
} | ||
} |