Skip to content

Commit 2821dd9

Browse files
Merge pull request #29 from mathewmeconry/feature/v3_jwt
Continues API v3 implementation
2 parents 03ae6a1 + 21afbdb commit 2821dd9

25 files changed

+3816
-2236
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,6 @@ lib/
6565

6666
# custom exlusion
6767
src/test.ts
68-
.vscode/
68+
.vscode/
69+
jest_html_reporters.html
70+
junit.xml

package.json

+43-24
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,13 @@
1111
"office.bexio.com"
1212
],
1313
"scripts": {
14-
"test": "nyc mocha --require ts-node/register --require source-map-support/register --recursive ./src/tests/*.test.ts --exit",
15-
"report": "nyc report --reporter=text-lcov | ./node_modules/.bin/codecov --pipe",
14+
"test": "jest",
15+
"report": "./node_modules/.bin/codecov",
1616
"build": "./node_modules/.bin/tsc",
1717
"prepare": "npm run build",
1818
"version": "git add -A src",
1919
"postversion": "git push && git push --tags"
2020
},
21-
"nyc": {
22-
"extends": "@istanbuljs/nyc-config-typescript",
23-
"exclude": [
24-
"src/interfaces/**",
25-
"src/constants/**",
26-
"src/tests/**",
27-
"lib/tests/**"
28-
],
29-
"all": true,
30-
"check-coverage": true
31-
},
3221
"repository": {
3322
"type": "git",
3423
"url": "git+https://github.com/mathewmeconry/bexio.git"
@@ -43,19 +32,49 @@
4332
"axios": "^0.20.0"
4433
},
4534
"devDependencies": {
46-
"@istanbuljs/nyc-config-typescript": "^0.1.3",
47-
"@types/chai": "^4.1.7",
35+
"@types/chance": "^1.1.0",
4836
"@types/dotenv": "^6.1.1",
49-
"@types/mocha": "^5.2.7",
50-
"@types/nock": "^10.0.3",
37+
"@types/jest": "^26.0.10",
5138
"@types/node": "^12.6.2",
52-
"chai": "^4.2.0",
39+
"chance": "^1.1.7",
5340
"codecov": "^3.6.1",
54-
"dotenv": "^8.0.0",
55-
"mocha": "^6.1.4",
56-
"nyc": "^14.1.1",
57-
"source-map-support": "^0.5.12",
58-
"ts-node": "^8.3.0",
41+
"jest": "^26.4.2",
42+
"jest-html-reporters": "^2.0.3",
43+
"jest-junit": "^11.1.0",
44+
"ts-jest": "^26.3.0",
5945
"typescript": "^4.0.2"
46+
},
47+
"jest": {
48+
"collectCoverage":true,
49+
"transform": {
50+
".(ts|tsx)": "ts-jest"
51+
},
52+
"testMatch": [
53+
"**/*.test.ts"
54+
],
55+
"moduleFileExtensions": [
56+
"ts",
57+
"tsx",
58+
"js",
59+
"jsx",
60+
"feature"
61+
],
62+
"reporters": [
63+
"default",
64+
"jest-html-reporters",
65+
"jest-junit"
66+
],
67+
"coverageDirectory": "coverage",
68+
"coverageReporters": [
69+
"html"
70+
],
71+
"collectCoverageFrom": [
72+
"src/**/*.{ts,tsx}",
73+
"!**/node_modules/**",
74+
"!**/coverage/**"
75+
],
76+
"roots": [
77+
"./src"
78+
]
6079
}
61-
}
80+
}

src/resources/BaseCrud.ts

+26-23
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,25 @@ export default class BaseCrud<
2121
/**
2222
* Lists the ressource
2323
*
24-
* @param {BaseStatic.BaseOptions} options
24+
* @param {BaseStatic.BaseOptions} [options]
2525
* @returns {Promise<Array<T>>}
2626
* @memberof BaseCrud
2727
*/
28-
public async list(options: BaseStatic.BaseOptions): Promise<Array<Small>> {
28+
public async list(options?: BaseStatic.BaseOptions): Promise<Array<Small>> {
2929
return this.request<Array<Small>>("GET", this.apiEndpoint, options);
3030
}
3131

3232
/**
3333
* search for resources
3434
*
35-
* @param {BaseStatic.BaseOptions} options
3635
* @param {Array<BaseStatic.SearchParameter<SearchType>>} searchOptions
36+
* @param {BaseStatic.BaseOptions} [options]
3737
* @returns {Promise<Array<Search>>}
3838
* @memberof BaseCrud
3939
*/
4040
public async search(
41-
options: BaseStatic.BaseOptions,
42-
searchOptions: Array<BaseStatic.SearchParameter<SearchType>>
41+
searchOptions: Array<BaseStatic.SearchParameter<SearchType>>,
42+
options?: BaseStatic.BaseOptions
4343
): Promise<Array<Search>> {
4444
return this.request<Array<Search>>(
4545
"POST",
@@ -52,14 +52,14 @@ export default class BaseCrud<
5252
/**
5353
* show a specific ressource
5454
*
55-
* @param {BaseStatic.BaseOptions} options
5655
* @param {number} id
56+
* @param {BaseStatic.BaseOptions} [options]
5757
* @returns {Promise<Full>}
5858
* @memberof BaseCrud
5959
*/
6060
public async show(
61-
options: BaseStatic.BaseOptions,
62-
id: number
61+
id: number,
62+
options?: BaseStatic.BaseOptions
6363
): Promise<Full> {
6464
return this.request<Full>("GET", this.apiEndpoint + "/" + id, options);
6565
}
@@ -72,7 +72,7 @@ export default class BaseCrud<
7272
* @memberof BaseCrud
7373
*/
7474
public async create(ressource: Create): Promise<Full> {
75-
return this.request<Full>("POST", this.apiEndpoint, {}, ressource);
75+
return this.request<Full>("POST", this.apiEndpoint, undefined, ressource);
7676
}
7777

7878
/**
@@ -87,7 +87,7 @@ export default class BaseCrud<
8787
return this.request<Full>(
8888
"PUT",
8989
this.apiEndpoint + "/" + id,
90-
{},
90+
undefined,
9191
ressource
9292
);
9393
}
@@ -104,7 +104,7 @@ export default class BaseCrud<
104104
return this.request<Full>(
105105
"POST",
106106
this.apiEndpoint + "/" + id,
107-
{},
107+
undefined,
108108
ressource
109109
);
110110
}
@@ -120,8 +120,7 @@ export default class BaseCrud<
120120
return (
121121
await this.request<{ success: boolean }>(
122122
"DELETE",
123-
this.apiEndpoint + "/" + id,
124-
{}
123+
this.apiEndpoint + "/" + id
125124
)
126125
).success;
127126
}
@@ -133,7 +132,7 @@ export default class BaseCrud<
133132
* @template T
134133
* @param {string} method
135134
* @param {string} path
136-
* @param {BaseStatic.BaseOptions} options
135+
* @param {BaseStatic.BaseOptions} [options]
137136
* @param {*} [data]
138137
* @returns {Promise<T>}
139138
* @memberof Bexio
@@ -161,12 +160,12 @@ export default class BaseCrud<
161160
| "unlink"
162161
| "UNLINK",
163162
path: string,
164-
options: BaseStatic.BaseOptions,
163+
options?: BaseStatic.BaseOptions,
165164
data?: any
166165
): Promise<T> {
167166
let requestOptions: AxiosRequestConfig = {
168167
method: method,
169-
url: this.baseApiUrl + path + "?" + this.optionsToQuery(options),
168+
url: this.baseApiUrl + path + this.optionsToQuery(options),
170169
headers: {
171170
Authorization: `Bearer ${this.apiToken}`,
172171
"Content-Type": "application/json",
@@ -180,33 +179,37 @@ export default class BaseCrud<
180179
}
181180

182181
try {
183-
const reponse = await axios(requestOptions);
182+
const reponse = await axios.request(requestOptions);
184183
return reponse.data;
185184
} catch (e) {
186185
const error = e as AxiosError;
187-
return Promise.reject(
188-
`Bexio request failed with status code ${error.response?.status} and message ${JSON.stringify(error.response?.data)}`
189-
);
186+
return Promise.reject({
187+
code: error.response?.status,
188+
message: error.response?.data,
189+
});
190190
}
191191
}
192192

193193
/**
194194
* Generates the querystring out of the options
195195
*
196196
* @protected
197-
* @param {BaseStatic.BaseOptions} options
197+
* @param {BaseStatic.BaseOptions} [options]
198198
* @returns {string}
199199
* @memberof Bexio
200200
*/
201-
protected optionsToQuery(options: BaseStatic.BaseOptions): string {
201+
protected optionsToQuery(options?: BaseStatic.BaseOptions): string {
202202
let str = [];
203+
if (!options) {
204+
return "";
205+
}
203206

204207
for (let i in options) {
205208
if (options.hasOwnProperty(i)) {
206209
str.push(encodeURIComponent(i) + "=" + encodeURIComponent(options[i]));
207210
}
208211
}
209212

210-
return str.join("&");
213+
return `?${str.join("&")}`;
211214
}
212215
}

src/resources/Bills.ts

+10-12
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ export default class Bills extends BaseCrud<
2626
public async issue(id: number): Promise<{ success: boolean }> {
2727
return this.request<{ success: boolean }>(
2828
"POST",
29-
`${this.apiEndpoint}/${id.toString()}/issue`,
30-
{}
29+
`${this.apiEndpoint}/${id.toString()}/issue`
3130
);
3231
}
3332

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

4947
/**
5048
* List all payments for this bill
5149
*
52-
* @param {BaseStatic.BaseOptions} options
5350
* @param {number} billId
51+
* @param {BaseStatic.BaseOptions} [options]
5452
* @returns {Promise<PaymentsStatic.Payment[]>}
5553
* @memberof Bills
5654
*/
5755
public async listPayments(
58-
options: BaseStatic.BaseOptions,
59-
billId: number
56+
billId: number,
57+
options?: BaseStatic.BaseOptions
6058
): Promise<PaymentsStatic.Payment[]> {
6159
const paymentCrud = new Payments(this.apiToken, billId);
62-
return paymentCrud.list({});
60+
return paymentCrud.list(options);
6361
}
6462

6563
/**
6664
* Show a specific payment for this bill
6765
*
68-
* @param {BaseStatic.BaseOptions} options
6966
* @param {number} billId
7067
* @param {number} paymentId
68+
* @param {BaseStatic.BaseOptions} [options]
7169
* @returns {Promise<PaymentsStatic.Payment>}
7270
* @memberof Bills
7371
*/
7472
public async showPayment(
75-
options: BaseStatic.BaseOptions,
7673
billId: number,
77-
paymentId: number
74+
paymentId: number,
75+
options?: BaseStatic.BaseOptions,
7876
): Promise<PaymentsStatic.Payment> {
7977
const paymentCrud = new Payments(this.apiToken, billId);
80-
return paymentCrud.show({}, paymentId);
78+
return paymentCrud.show(paymentId, options);
8179
}
8280

8381
/**

src/resources/ContactRelations.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ export default class ContactRelations extends BaseCrud<
1010
ContactRelationsStatic.ContactRelationOverwrite
1111
> {
1212
constructor(apiToken: string) {
13-
super(apiToken, "/kb_bill");
13+
super(apiToken, "/contact_relation");
1414
}
1515
}

src/resources/Expenses.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import BaseCrud from "./BaseCrud";
22
import { ExpensesStatic } from "../interfaces/ExpensesStatic";
33

4-
export default class Contacts extends BaseCrud<
4+
export default class Expenses extends BaseCrud<
55
ExpensesStatic.Expense,
66
ExpensesStatic.Expense,
77
ExpensesStatic.Expense,

0 commit comments

Comments
 (0)