Skip to content

Commit

Permalink
Merge pull request #29 from mathewmeconry/feature/v3_jwt
Browse files Browse the repository at this point in the history
Continues API v3 implementation
  • Loading branch information
mathewmeconry authored Aug 31, 2020
2 parents 03ae6a1 + 21afbdb commit 2821dd9
Show file tree
Hide file tree
Showing 25 changed files with 3,816 additions and 2,236 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,6 @@ lib/

# custom exlusion
src/test.ts
.vscode/
.vscode/
jest_html_reporters.html
junit.xml
67 changes: 43 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,13 @@
"office.bexio.com"
],
"scripts": {
"test": "nyc mocha --require ts-node/register --require source-map-support/register --recursive ./src/tests/*.test.ts --exit",
"report": "nyc report --reporter=text-lcov | ./node_modules/.bin/codecov --pipe",
"test": "jest",
"report": "./node_modules/.bin/codecov",
"build": "./node_modules/.bin/tsc",
"prepare": "npm run build",
"version": "git add -A src",
"postversion": "git push && git push --tags"
},
"nyc": {
"extends": "@istanbuljs/nyc-config-typescript",
"exclude": [
"src/interfaces/**",
"src/constants/**",
"src/tests/**",
"lib/tests/**"
],
"all": true,
"check-coverage": true
},
"repository": {
"type": "git",
"url": "git+https://github.com/mathewmeconry/bexio.git"
Expand All @@ -43,19 +32,49 @@
"axios": "^0.20.0"
},
"devDependencies": {
"@istanbuljs/nyc-config-typescript": "^0.1.3",
"@types/chai": "^4.1.7",
"@types/chance": "^1.1.0",
"@types/dotenv": "^6.1.1",
"@types/mocha": "^5.2.7",
"@types/nock": "^10.0.3",
"@types/jest": "^26.0.10",
"@types/node": "^12.6.2",
"chai": "^4.2.0",
"chance": "^1.1.7",
"codecov": "^3.6.1",
"dotenv": "^8.0.0",
"mocha": "^6.1.4",
"nyc": "^14.1.1",
"source-map-support": "^0.5.12",
"ts-node": "^8.3.0",
"jest": "^26.4.2",
"jest-html-reporters": "^2.0.3",
"jest-junit": "^11.1.0",
"ts-jest": "^26.3.0",
"typescript": "^4.0.2"
},
"jest": {
"collectCoverage":true,
"transform": {
".(ts|tsx)": "ts-jest"
},
"testMatch": [
"**/*.test.ts"
],
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"feature"
],
"reporters": [
"default",
"jest-html-reporters",
"jest-junit"
],
"coverageDirectory": "coverage",
"coverageReporters": [
"html"
],
"collectCoverageFrom": [
"src/**/*.{ts,tsx}",
"!**/node_modules/**",
"!**/coverage/**"
],
"roots": [
"./src"
]
}
}
}
49 changes: 26 additions & 23 deletions src/resources/BaseCrud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,25 @@ export default class BaseCrud<
/**
* Lists the ressource
*
* @param {BaseStatic.BaseOptions} options
* @param {BaseStatic.BaseOptions} [options]
* @returns {Promise<Array<T>>}
* @memberof BaseCrud
*/
public async list(options: BaseStatic.BaseOptions): Promise<Array<Small>> {
public async list(options?: BaseStatic.BaseOptions): Promise<Array<Small>> {
return this.request<Array<Small>>("GET", this.apiEndpoint, options);
}

/**
* search for resources
*
* @param {BaseStatic.BaseOptions} options
* @param {Array<BaseStatic.SearchParameter<SearchType>>} searchOptions
* @param {BaseStatic.BaseOptions} [options]
* @returns {Promise<Array<Search>>}
* @memberof BaseCrud
*/
public async search(
options: BaseStatic.BaseOptions,
searchOptions: Array<BaseStatic.SearchParameter<SearchType>>
searchOptions: Array<BaseStatic.SearchParameter<SearchType>>,
options?: BaseStatic.BaseOptions
): Promise<Array<Search>> {
return this.request<Array<Search>>(
"POST",
Expand All @@ -52,14 +52,14 @@ export default class BaseCrud<
/**
* show a specific ressource
*
* @param {BaseStatic.BaseOptions} options
* @param {number} id
* @param {BaseStatic.BaseOptions} [options]
* @returns {Promise<Full>}
* @memberof BaseCrud
*/
public async show(
options: BaseStatic.BaseOptions,
id: number
id: number,
options?: BaseStatic.BaseOptions
): Promise<Full> {
return this.request<Full>("GET", this.apiEndpoint + "/" + id, options);
}
Expand All @@ -72,7 +72,7 @@ export default class BaseCrud<
* @memberof BaseCrud
*/
public async create(ressource: Create): Promise<Full> {
return this.request<Full>("POST", this.apiEndpoint, {}, ressource);
return this.request<Full>("POST", this.apiEndpoint, undefined, ressource);
}

/**
Expand All @@ -87,7 +87,7 @@ export default class BaseCrud<
return this.request<Full>(
"PUT",
this.apiEndpoint + "/" + id,
{},
undefined,
ressource
);
}
Expand All @@ -104,7 +104,7 @@ export default class BaseCrud<
return this.request<Full>(
"POST",
this.apiEndpoint + "/" + id,
{},
undefined,
ressource
);
}
Expand All @@ -120,8 +120,7 @@ export default class BaseCrud<
return (
await this.request<{ success: boolean }>(
"DELETE",
this.apiEndpoint + "/" + id,
{}
this.apiEndpoint + "/" + id
)
).success;
}
Expand All @@ -133,7 +132,7 @@ export default class BaseCrud<
* @template T
* @param {string} method
* @param {string} path
* @param {BaseStatic.BaseOptions} options
* @param {BaseStatic.BaseOptions} [options]
* @param {*} [data]
* @returns {Promise<T>}
* @memberof Bexio
Expand Down Expand Up @@ -161,12 +160,12 @@ export default class BaseCrud<
| "unlink"
| "UNLINK",
path: string,
options: BaseStatic.BaseOptions,
options?: BaseStatic.BaseOptions,
data?: any
): Promise<T> {
let requestOptions: AxiosRequestConfig = {
method: method,
url: this.baseApiUrl + path + "?" + this.optionsToQuery(options),
url: this.baseApiUrl + path + this.optionsToQuery(options),
headers: {
Authorization: `Bearer ${this.apiToken}`,
"Content-Type": "application/json",
Expand All @@ -180,33 +179,37 @@ export default class BaseCrud<
}

try {
const reponse = await axios(requestOptions);
const reponse = await axios.request(requestOptions);
return reponse.data;
} catch (e) {
const error = e as AxiosError;
return Promise.reject(
`Bexio request failed with status code ${error.response?.status} and message ${JSON.stringify(error.response?.data)}`
);
return Promise.reject({
code: error.response?.status,
message: error.response?.data,
});
}
}

/**
* Generates the querystring out of the options
*
* @protected
* @param {BaseStatic.BaseOptions} options
* @param {BaseStatic.BaseOptions} [options]
* @returns {string}
* @memberof Bexio
*/
protected optionsToQuery(options: BaseStatic.BaseOptions): string {
protected optionsToQuery(options?: BaseStatic.BaseOptions): string {
let str = [];
if (!options) {
return "";
}

for (let i in options) {
if (options.hasOwnProperty(i)) {
str.push(encodeURIComponent(i) + "=" + encodeURIComponent(options[i]));
}
}

return str.join("&");
return `?${str.join("&")}`;
}
}
22 changes: 10 additions & 12 deletions src/resources/Bills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ export default class Bills extends BaseCrud<
public async issue(id: number): Promise<{ success: boolean }> {
return this.request<{ success: boolean }>(
"POST",
`${this.apiEndpoint}/${id.toString()}/issue`,
{}
`${this.apiEndpoint}/${id.toString()}/issue`
);
}

Expand All @@ -41,43 +40,42 @@ export default class Bills extends BaseCrud<
public async revertIssue(id: number): Promise<{ success: boolean }> {
return this.request<{ success: boolean }>(
"POST",
`${this.apiEndpoint}/${id.toString()}/revert_issue`,
{}
`${this.apiEndpoint}/${id.toString()}/revert_issue`
);
}

/**
* List all payments for this bill
*
* @param {BaseStatic.BaseOptions} options
* @param {number} billId
* @param {BaseStatic.BaseOptions} [options]
* @returns {Promise<PaymentsStatic.Payment[]>}
* @memberof Bills
*/
public async listPayments(
options: BaseStatic.BaseOptions,
billId: number
billId: number,
options?: BaseStatic.BaseOptions
): Promise<PaymentsStatic.Payment[]> {
const paymentCrud = new Payments(this.apiToken, billId);
return paymentCrud.list({});
return paymentCrud.list(options);
}

/**
* Show a specific payment for this bill
*
* @param {BaseStatic.BaseOptions} options
* @param {number} billId
* @param {number} paymentId
* @param {BaseStatic.BaseOptions} [options]
* @returns {Promise<PaymentsStatic.Payment>}
* @memberof Bills
*/
public async showPayment(
options: BaseStatic.BaseOptions,
billId: number,
paymentId: number
paymentId: number,
options?: BaseStatic.BaseOptions,
): Promise<PaymentsStatic.Payment> {
const paymentCrud = new Payments(this.apiToken, billId);
return paymentCrud.show({}, paymentId);
return paymentCrud.show(paymentId, options);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/resources/ContactRelations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ export default class ContactRelations extends BaseCrud<
ContactRelationsStatic.ContactRelationOverwrite
> {
constructor(apiToken: string) {
super(apiToken, "/kb_bill");
super(apiToken, "/contact_relation");
}
}
2 changes: 1 addition & 1 deletion src/resources/Expenses.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import BaseCrud from "./BaseCrud";
import { ExpensesStatic } from "../interfaces/ExpensesStatic";

export default class Contacts extends BaseCrud<
export default class Expenses extends BaseCrud<
ExpensesStatic.Expense,
ExpensesStatic.Expense,
ExpensesStatic.Expense,
Expand Down
Loading

0 comments on commit 2821dd9

Please sign in to comment.