@jphil/bookwhen-client 0.0.10 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -8
- package/dist/client/BookwhenClient.d.ts +38 -0
- package/dist/client/BookwhenClient.integration.test.d.ts +1 -0
- package/dist/client/BookwhenClient.integration.test.js +78 -0
- package/dist/client/BookwhenClient.integration.test.js.map +1 -0
- package/dist/client/BookwhenClient.js +69 -0
- package/dist/client/BookwhenClient.js.map +1 -0
- package/dist/client/BookwhenClient.unit.test.d.ts +1 -0
- package/dist/client/BookwhenClient.unit.test.js +70 -0
- package/dist/client/BookwhenClient.unit.test.js.map +1 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/dist/index.test.js +4 -3
- package/dist/index.test.js.map +1 -1
- package/dist/request/BookwhenRequest.d.ts +46 -0
- package/dist/request/BookwhenRequest.js +126 -0
- package/dist/request/BookwhenRequest.js.map +1 -0
- package/dist/request/BookwhenRequest.unit.test.d.ts +1 -0
- package/dist/request/BookwhenRequest.unit.test.js +141 -0
- package/dist/request/BookwhenRequest.unit.test.js.map +1 -0
- package/dist/request/httpStatusCodes.d.ts +3 -0
- package/dist/request/httpStatusCodes.js +15 -0
- package/dist/request/httpStatusCodes.js.map +1 -0
- package/dist/services/event/Event.d.ts +32 -0
- package/dist/services/event/Event.js +72 -0
- package/dist/services/event/Event.js.map +1 -0
- package/dist/services/event/Event.unit.test.d.ts +1 -0
- package/dist/services/event/Event.unit.test.js +104 -0
- package/dist/services/event/Event.unit.test.js.map +1 -0
- package/dist/services/event/EventInterfaces.d.ts +53 -0
- package/dist/services/event/EventInterfaces.js +3 -0
- package/dist/services/event/EventInterfaces.js.map +1 -0
- package/dist/services/event/EventSchemas.d.ts +12 -0
- package/dist/services/event/EventSchemas.js +8 -0
- package/dist/services/event/EventSchemas.js.map +1 -0
- package/dist/services/event/EventTypes.d.ts +36 -0
- package/dist/services/event/EventTypes.js +2 -0
- package/dist/services/event/EventTypes.js.map +1 -0
- package/dist/types/GlobalTypes.d.ts +9 -0
- package/dist/types/GlobalTypes.js +2 -0
- package/dist/types/GlobalTypes.js.map +1 -0
- package/dist/utils/http-utils.d.ts +8 -0
- package/dist/utils/http-utils.js +19 -0
- package/dist/utils/http-utils.js.map +1 -0
- package/package.json +11 -2
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
\[wip\]!
|
|
4
4
|
|
|
5
|
-
An API client library for the [Bookwhen](www.bookwhen.com) booking platform [API (v2)](https://api.bookwhen.com/v2), written in Typescript for NodeJS. Intended for server-side environments.
|
|
5
|
+
An API client library for the [Bookwhen](https://www.bookwhen.com) booking platform [API (v2)](https://api.bookwhen.com/v2), written in Typescript for NodeJS. Intended for server-side environments.
|
|
6
6
|
|
|
7
7
|
## Table of Contents
|
|
8
8
|
|
|
@@ -17,12 +17,12 @@ An API client library for the [Bookwhen](www.bookwhen.com) booking platform [API
|
|
|
17
17
|
|
|
18
18
|
## Overview
|
|
19
19
|
|
|
20
|
-
You'll likely be at least somewhat familiar with the [Bookwhen](www.bookwhen.com) booking platform if you've landed here. But if not, you'll want to have a look at their [API (v2) documentation](https://api.bookwhen.com/v2). There's also a nice [Swagger style layout of the Bookwhen API v2 docs](https://petstore.swagger.io/?url=https://api.bookwhen.com/v2/openapi.yaml)
|
|
20
|
+
You'll likely be at least somewhat familiar with the [Bookwhen](https://www.bookwhen.com) booking platform if you've landed here. But if not, you'll want to have a look at their [API (v2) documentation](https://api.bookwhen.com/v2). There's also a nice [Swagger style layout of the Bookwhen API v2 docs](https://petstore.swagger.io/?url=https://api.bookwhen.com/v2/openapi.yaml)
|
|
21
21
|
|
|
22
22
|
## Features
|
|
23
23
|
|
|
24
|
-
-
|
|
25
|
-
-
|
|
24
|
+
- Provides an easy way to pull your data from Bookwhen for NodeJS environments
|
|
25
|
+
- Provides fully typed methods for each model (so far just the events model) in the Bookwhen API v2
|
|
26
26
|
|
|
27
27
|
## Installation
|
|
28
28
|
|
|
@@ -34,10 +34,18 @@ pnpm add @jphil/bookwhen-client
|
|
|
34
34
|
|
|
35
35
|
## Usage
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
!N.B. this usage structure may change as I progress towards a 1.0.0
|
|
38
38
|
|
|
39
39
|
```typescript
|
|
40
|
-
|
|
40
|
+
import { createBookwhenClient } from 'bookwhen-client';
|
|
41
|
+
|
|
42
|
+
const client = createBookwhenClient(YOUR_API_KEY)
|
|
43
|
+
const event = client.event.getByID({myEventId});
|
|
44
|
+
const events = client.event.getMultiple({
|
|
45
|
+
filters: ["filter1", "filter2"]
|
|
46
|
+
includes: ["inc1", "inc2"]
|
|
47
|
+
});
|
|
48
|
+
|
|
41
49
|
```
|
|
42
50
|
|
|
43
51
|
## Configuration
|
|
@@ -56,9 +64,15 @@ Please see the docs in the CONTRIBUTIONS.md file, thanks!
|
|
|
56
64
|
|
|
57
65
|
## Roadmap
|
|
58
66
|
|
|
59
|
-
- Keep up with any future changes or additions to the [Bookwhen API](https://api.bookwhen.com/v2).
|
|
67
|
+
- Keep up with any future changes or additions to the [Bookwhen API](https://api.bookwhen.com/v2), additions will be driven mainly by this.
|
|
68
|
+
- Possibly add a "fields" param to service methods to allow response filtering
|
|
69
|
+
|
|
70
|
+
### Todos
|
|
71
|
+
- [] put Zod in place in more areas to strengthen runtime type guards
|
|
72
|
+
- [] refine error handling
|
|
73
|
+
- [] write more integration tests
|
|
74
|
+
- [] write services for the other integrations
|
|
60
75
|
|
|
61
76
|
## License
|
|
62
77
|
|
|
63
78
|
ISC License. See [LICENSE](LICENSE) for more information.
|
|
64
|
-
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { AxiosInstance } from 'axios';
|
|
2
|
+
import { EventService } from '../services/event/Event.js';
|
|
3
|
+
/**
|
|
4
|
+
* Client for the Bookwhen API.
|
|
5
|
+
*
|
|
6
|
+
* @see https://petstore.swagger.io/?url=https://api.bookwhen.com/v2/openapi.yaml#/ClassPass/get_class_passes__class_pass_id_
|
|
7
|
+
*/
|
|
8
|
+
export declare class BookwhenClient {
|
|
9
|
+
private axiosInstance;
|
|
10
|
+
private eventService?;
|
|
11
|
+
/**
|
|
12
|
+
* Creates a new instance of the BookwhenClient class.
|
|
13
|
+
* @param axiosInstance - Configured Axios instance for making HTTP requests.
|
|
14
|
+
* @throws Error if axiosInstance is not provided.
|
|
15
|
+
*/
|
|
16
|
+
constructor(axiosInstance: AxiosInstance);
|
|
17
|
+
/**
|
|
18
|
+
* Gets the EventService instance.
|
|
19
|
+
*
|
|
20
|
+
* Available methods:
|
|
21
|
+
* - getById(params: GetEventByIdParams): Promise<BookwhenEvent>
|
|
22
|
+
* - getMultiple(params: GetMultipleEventsParams): Promise<BookwhenEvent[]>
|
|
23
|
+
*
|
|
24
|
+
* @returns The EventService instance.
|
|
25
|
+
*/
|
|
26
|
+
get events(): EventService;
|
|
27
|
+
}
|
|
28
|
+
interface BookwhenClientOptions {
|
|
29
|
+
apiKey: string;
|
|
30
|
+
baseURL?: string;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Creates an instance of Axios with the provided API key.
|
|
34
|
+
* @param apiKey - The API key used for authentication.
|
|
35
|
+
* @returns The Axios instance.
|
|
36
|
+
*/
|
|
37
|
+
export declare function createBookwhenClient(options: BookwhenClientOptions): BookwhenClient;
|
|
38
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
import { BookwhenClient } from './BookwhenClient.js';
|
|
3
|
+
import { EventService } from '../services/event/Event.js';
|
|
4
|
+
vi.mock('axios');
|
|
5
|
+
vi.mock('../src/services/event/Event');
|
|
6
|
+
describe('BookwhenClient Integration', () => {
|
|
7
|
+
describe('BookwhenClient - Events Service', () => {
|
|
8
|
+
it('should call the correct endpoint when getById is called', async () => {
|
|
9
|
+
const mockAxiosInstance = {
|
|
10
|
+
get: vi.fn().mockResolvedValue({
|
|
11
|
+
data: {
|
|
12
|
+
data: { id: 'event123' }
|
|
13
|
+
}
|
|
14
|
+
}),
|
|
15
|
+
};
|
|
16
|
+
const client = new BookwhenClient(mockAxiosInstance);
|
|
17
|
+
const eventId = 'event123';
|
|
18
|
+
const event = await client.events.getById({ eventId });
|
|
19
|
+
expect(mockAxiosInstance.get).toHaveBeenCalledWith('/events/event123');
|
|
20
|
+
expect(event).toEqual({ id: 'event123' });
|
|
21
|
+
});
|
|
22
|
+
it('should call the correct endpoint with correct parameters when events.getMultiple is called', async () => {
|
|
23
|
+
const mockAxiosInstance = {
|
|
24
|
+
get: vi.fn().mockResolvedValue({
|
|
25
|
+
data: {
|
|
26
|
+
data: [{ id: 'event1' }, { id: 'event2' }],
|
|
27
|
+
},
|
|
28
|
+
}),
|
|
29
|
+
};
|
|
30
|
+
const client = new BookwhenClient(mockAxiosInstance);
|
|
31
|
+
const includes = ['location', 'tickets'];
|
|
32
|
+
const filters = {
|
|
33
|
+
title: ['Workshop'],
|
|
34
|
+
start_at: ['2023-01-01']
|
|
35
|
+
};
|
|
36
|
+
const eventsData = [
|
|
37
|
+
{ id: '1', type: 'event' },
|
|
38
|
+
{ id: '2', type: 'event' },
|
|
39
|
+
];
|
|
40
|
+
const events = await client.events.getMultiple({ includes, filters });
|
|
41
|
+
expect(mockAxiosInstance.get).toHaveBeenCalledWith('/events?filter[title]=Workshop&filter[start_at]=2023-01-01&include=location,tickets');
|
|
42
|
+
expect(events).toEqual([{ id: 'event1' }, { id: 'event2' }]);
|
|
43
|
+
});
|
|
44
|
+
it('should correctly initialize and expose the events service via the client', () => {
|
|
45
|
+
const mockAxiosInstance = {};
|
|
46
|
+
const client = new BookwhenClient(mockAxiosInstance);
|
|
47
|
+
const eventsService = client.events;
|
|
48
|
+
expect(eventsService).toBeInstanceOf(EventService);
|
|
49
|
+
});
|
|
50
|
+
it('should use the same axios instance in events service as in the BookwhenClient', () => {
|
|
51
|
+
const mockAxiosInstance = {};
|
|
52
|
+
const client = new BookwhenClient(mockAxiosInstance);
|
|
53
|
+
const eventsService = client.events;
|
|
54
|
+
expect(eventsService['axiosInstance']).toBe(client['axiosInstance']);
|
|
55
|
+
});
|
|
56
|
+
it('should handle errors thrown by the events service methods', async () => {
|
|
57
|
+
const mockAxiosInstance = {
|
|
58
|
+
get: vi.fn().mockRejectedValue(new Error('Network Error')),
|
|
59
|
+
};
|
|
60
|
+
const client = new BookwhenClient(mockAxiosInstance);
|
|
61
|
+
// @ts-ignore - Testing invalid parameter type
|
|
62
|
+
await expect(client.events.getById(453453)).rejects.toThrow('events.getById: Schema Validation failed: Expected object, received number');
|
|
63
|
+
// @ts-ignore - Testing invalid parameter type
|
|
64
|
+
await expect(client.events.getById('invalidId')).rejects.toThrow('events.getById: Schema Validation failed: Expected object, received string');
|
|
65
|
+
});
|
|
66
|
+
it('should correctly pass filters and includes to events.getMultiple method', async () => {
|
|
67
|
+
const mockAxiosInstance = {
|
|
68
|
+
get: vi.fn().mockResolvedValue({ data: { data: [] } }),
|
|
69
|
+
};
|
|
70
|
+
const client = new BookwhenClient(mockAxiosInstance);
|
|
71
|
+
const filters = { tag: ['workshop', 'seminar'], from: '20220101' };
|
|
72
|
+
const includes = ['location', 'tickets.events'];
|
|
73
|
+
await client.events.getMultiple({ filters, includes });
|
|
74
|
+
expect(mockAxiosInstance.get).toHaveBeenCalledWith('/events?filter[tag]=workshop,seminar&filter[from]=20220101&include=location,tickets.events');
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
//# sourceMappingURL=BookwhenClient.integration.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BookwhenClient.integration.test.js","sourceRoot":"","sources":["../../src/client/BookwhenClient.integration.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,E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M,CAAC,WAAW,CAAC,EAAE,OAAO,EAAE,QAAQ,EAAE,CAAC,CAAC;YAEvD,MAAM,CAAC,iBAAiB,CAAC,GAAG,CAAC,CAAC,oBAAoB,CAAC,4FAA4F,CAAC,CAAC;QACnJ,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// src/clients/BookwhenClient.ts
|
|
2
|
+
import axios from 'axios';
|
|
3
|
+
import { EventService } from '../services/event/Event.js'; // Ensure this file and class are setup accordingly
|
|
4
|
+
import { CLIENT_HTTP_STATUS_CODES } from '../request/httpStatusCodes.js';
|
|
5
|
+
/**
|
|
6
|
+
* Client for the Bookwhen API.
|
|
7
|
+
*
|
|
8
|
+
* @see https://petstore.swagger.io/?url=https://api.bookwhen.com/v2/openapi.yaml#/ClassPass/get_class_passes__class_pass_id_
|
|
9
|
+
*/
|
|
10
|
+
export class BookwhenClient {
|
|
11
|
+
axiosInstance;
|
|
12
|
+
eventService;
|
|
13
|
+
/**
|
|
14
|
+
* Creates a new instance of the BookwhenClient class.
|
|
15
|
+
* @param axiosInstance - Configured Axios instance for making HTTP requests.
|
|
16
|
+
* @throws Error if axiosInstance is not provided.
|
|
17
|
+
*/
|
|
18
|
+
constructor(axiosInstance) {
|
|
19
|
+
this.axiosInstance = axiosInstance;
|
|
20
|
+
if (!axiosInstance) {
|
|
21
|
+
throw new Error('BookwhenClient - you must provide an axios instance');
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Gets the EventService instance.
|
|
26
|
+
*
|
|
27
|
+
* Available methods:
|
|
28
|
+
* - getById(params: GetEventByIdParams): Promise<BookwhenEvent>
|
|
29
|
+
* - getMultiple(params: GetMultipleEventsParams): Promise<BookwhenEvent[]>
|
|
30
|
+
*
|
|
31
|
+
* @returns The EventService instance.
|
|
32
|
+
*/
|
|
33
|
+
get events() {
|
|
34
|
+
if (!this.eventService) {
|
|
35
|
+
this.eventService = new EventService(this.axiosInstance);
|
|
36
|
+
}
|
|
37
|
+
return this.eventService;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Creates an instance of Axios with the provided API key.
|
|
42
|
+
* @param apiKey - The API key used for authentication.
|
|
43
|
+
* @returns The Axios instance.
|
|
44
|
+
*/
|
|
45
|
+
export function createBookwhenClient(options) {
|
|
46
|
+
const { apiKey, baseURL = 'https://api.bookwhen.com/v2' } = options;
|
|
47
|
+
const axiosInstance = axios.create({
|
|
48
|
+
baseURL: baseURL,
|
|
49
|
+
auth: { username: apiKey, password: '' },
|
|
50
|
+
});
|
|
51
|
+
axiosInstance.interceptors.response.use((response) => response.data ?? response, (error) => {
|
|
52
|
+
if (error.response) {
|
|
53
|
+
const status = error.response.status;
|
|
54
|
+
const clientError = CLIENT_HTTP_STATUS_CODES[status];
|
|
55
|
+
if (clientError) {
|
|
56
|
+
return Promise.reject(new Error(clientError.message));
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
else if (error.request) {
|
|
60
|
+
throw new Error('No response received from the server');
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
throw new Error('An error occurred setting up the request');
|
|
64
|
+
}
|
|
65
|
+
return Promise.reject(error);
|
|
66
|
+
});
|
|
67
|
+
return new BookwhenClient(axiosInstance);
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=BookwhenClient.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BookwhenClient.js","sourceRoot":"","sources":["../../src/client/BookwhenClient.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC,CAAE,mDAAmD;AAC/G,OAAO,EAAE,wBAAwB,EAAE,MAAM,+BAA+B,CAAC;AAEzE;;;;GAIG;AACH,MAAM,OAAO,cAAc;IAQL;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AI,KAAK,CAAC,sCAAsC,CAAC,CAAC;QAC1D,CAAC;aAAM,CAAC;YACN,MAAM,IAAI,KAAK,CAAC,0CAA0C,CAAC,CAAC;QAC9D,CAAC;QACD,OAAO,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC/B,CAAC,CACF,CAAC;IAEF,OAAO,IAAI,cAAc,CAAC,aAAa,CAAC,CAAC;AAC3C,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
import axios from 'axios';
|
|
3
|
+
import { createBookwhenClient } from './BookwhenClient.js';
|
|
4
|
+
import { BookwhenClient } from './BookwhenClient.js';
|
|
5
|
+
import { EventService } from '../services/event/Event.js';
|
|
6
|
+
vi.mock('axios');
|
|
7
|
+
vi.mock('../src/services/event/Event');
|
|
8
|
+
describe('BookwhenClient', () => {
|
|
9
|
+
it('should throw an error if axiosInstance is not provided', () => {
|
|
10
|
+
expect(() => new BookwhenClient(undefined)).toThrowError('BookwhenClient - you must provide an axios instance');
|
|
11
|
+
});
|
|
12
|
+
it('should assign axiosInstance correctly', () => {
|
|
13
|
+
const mockAxiosInstance = {};
|
|
14
|
+
const client = new BookwhenClient(mockAxiosInstance);
|
|
15
|
+
expect(client['axiosInstance']).toBe(mockAxiosInstance);
|
|
16
|
+
});
|
|
17
|
+
it('should expose EventService via getter', () => {
|
|
18
|
+
const mockAxiosInstance = {};
|
|
19
|
+
const client = new BookwhenClient(mockAxiosInstance);
|
|
20
|
+
const eventService = client.events;
|
|
21
|
+
expect(eventService['axiosInstance']).toBe(mockAxiosInstance);
|
|
22
|
+
expect(eventService).toBeInstanceOf(EventService);
|
|
23
|
+
});
|
|
24
|
+
it('should share axiosInstance with EventService', () => {
|
|
25
|
+
const mockAxiosInstance = {};
|
|
26
|
+
const client = new BookwhenClient(mockAxiosInstance);
|
|
27
|
+
const eventService = client.events;
|
|
28
|
+
expect(eventService['axiosInstance']).toBe(mockAxiosInstance);
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
describe('createBookwhenClient', () => {
|
|
32
|
+
let mockAxiosInstance;
|
|
33
|
+
beforeEach(() => {
|
|
34
|
+
// Create mock axios instance with interceptors
|
|
35
|
+
mockAxiosInstance = {
|
|
36
|
+
interceptors: {
|
|
37
|
+
response: {
|
|
38
|
+
use: vi.fn()
|
|
39
|
+
},
|
|
40
|
+
request: {
|
|
41
|
+
use: vi.fn()
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
// Mock axios.create to return our mock instance
|
|
46
|
+
vi.spyOn(axios, 'create').mockReturnValue(mockAxiosInstance);
|
|
47
|
+
});
|
|
48
|
+
it('should create an axiosInstance with correct config and return a BookwhenClient', () => {
|
|
49
|
+
const apiKey = 'test-api-key';
|
|
50
|
+
const client = createBookwhenClient({ apiKey });
|
|
51
|
+
expect(axios.create).toHaveBeenCalledWith({
|
|
52
|
+
baseURL: 'https://api.bookwhen.com/v2',
|
|
53
|
+
auth: { username: apiKey, password: '' },
|
|
54
|
+
});
|
|
55
|
+
expect(mockAxiosInstance.interceptors.response.use).toHaveBeenCalled();
|
|
56
|
+
expect(client).toBeInstanceOf(BookwhenClient);
|
|
57
|
+
});
|
|
58
|
+
it('should handle errors using the interceptor', async () => {
|
|
59
|
+
const axiosInstance = axios.create();
|
|
60
|
+
const errorResponse = { response: { status: 400 } };
|
|
61
|
+
vi.spyOn(axios, 'create').mockReturnValue(axiosInstance);
|
|
62
|
+
// Mock the interceptor
|
|
63
|
+
const interceptor = vi.fn();
|
|
64
|
+
axiosInstance.interceptors.response.use = interceptor;
|
|
65
|
+
createBookwhenClient({ apiKey: 'test-api-key' });
|
|
66
|
+
const [, errorHandler] = interceptor.mock.calls[0];
|
|
67
|
+
await expect(errorHandler(errorResponse)).rejects.toThrowError('BookwhenClient: Bad request');
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
//# sourceMappingURL=BookwhenClient.unit.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BookwhenClient.unit.test.js","sourceRoot":"","sources":["../../src/client/BookwhenClient.unit.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AAC9D,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,OAAO,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAC3D,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAE1D,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;AACjB,EAAE,CAAC,IAAI,CAAC,6BAA6B,CAAC,CAAC;AAEvC,QAAQ,CAAC,gBAAgB,EAAE,GAAG,EAAE;IAC9B,EAAE,CAAC,wDAAwD,EAAE,GAAG,EAAE;QAChE,MAAM,CAAC,GAAG,EAAE,CAAC,IAAI,cAAc,CAAC,SAAgB,CAAC,CAAC,CAAC,YAAY,CAC7D,qDAAqD,CACtD,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,uCAAuC,EAAE,GAAG,EAAE;QAC/C,MAAM,iBAAiB,GAAG,EAAmB,CAAC;QAC9C,MAAM,MAAM,GAAG,IAAI,cAAc,CAAC,iBAAiB,CAAC,CAAC;QAErD,MAAM,CAAC,MAAM,CAAC,eAAe,CAAC,CAAC,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC;IAC1D,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,uCAAuC,EAAE,GAAG,EAAE;QAC/C,MAAM,iBAAiB,GAAG,EAAmB,CAAC;QAC9C,MAAM,MAAM,GAAG,IAAI,cAAc,CAAC,iBAAiB,CAAC,CAAC;QAErD,MAAM,YAAY,GAAG,MAA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EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,EAAE,EAAE;SACzC,CAAC,CAAC;QACH,MAAM,CAAC,iBAAiB,CAAC,YAAY,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,gBAAgB,EAAE,CAAC;QACvE,MAAM,CAAC,MAAM,CAAC,CAAC,cAAc,CAAC,cAAc,CAAC,CAAC;IAChD,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,4CAA4C,EAAE,KAAK,IAAI,EAAE;QAC1D,MAAM,aAAa,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC;QACrC,MAAM,aAAa,GAAG,EAAE,QAAQ,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,EAAE,CAAC;QAEpD,EAAE,CAAC,KAAK,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC,eAAe,CAAC,aAAa,CAAC,CAAC;QAEzD,uBAAuB;QACvB,MAAM,WAAW,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC;QAC5B,aAAa,CAAC,YAAY,CAAC,QAAQ,CAAC,GAAG,GAAG,WAAW,CAAC;QAEtD,oBAAoB,CAAC,EAAE,MAAM,EAAE,cAAc,EAAE,CAAC,CAAC;QAEjD,MAAM,CAAC,EAAE,YAAY,CAAC,GAAG,WAAW,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAA8C,CAAC;QAEhG,MAAM,MAAM,CAAC,YAAY,CAAC,aAAa,CAAC,CAAC,CAAC,OAAO,CAAC,YAAY,CAAC,6BAA6B,CAAC,CAAC;IAChG,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
};
|
|
1
|
+
export * from './types/GlobalTypes.js';
|
|
2
|
+
export * from './services/event/EventInterfaces.js';
|
|
3
|
+
export { createBookwhenClient } from './client/BookwhenClient.js';
|
|
4
4
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC;AACvC,cAAc,qCAAqC,CAAC;AAEpD,OAAO,EAAE,oBAAoB,EAAE,MAAM,4BAA4B,CAAC"}
|
package/dist/index.test.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { describe, it, expect } from 'vitest';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
import { createBookwhenClient } from './index.js';
|
|
3
|
+
describe('index.ts', () => {
|
|
4
|
+
it('should export BookwhenClient', () => {
|
|
5
|
+
expect(createBookwhenClient).toBeDefined();
|
|
5
6
|
});
|
|
6
7
|
});
|
|
7
8
|
//# sourceMappingURL=index.test.js.map
|
package/dist/index.test.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.test.js","sourceRoot":"","sources":["../src/index.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;
|
|
1
|
+
{"version":3,"file":"index.test.js","sourceRoot":"","sources":["../src/index.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAC9C,OAAO,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAElD,QAAQ,CAAC,UAAU,EAAE,GAAG,EAAE;IACtB,EAAE,CAAC,8BAA8B,EAAE,GAAG,EAAE;QACpC,MAAM,CAAC,oBAAoB,CAAC,CAAC,WAAW,EAAE,CAAC;IAC/C,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { Resources, Filters } from "../types/GlobalTypes.js";
|
|
2
|
+
/**
|
|
3
|
+
* QueryBuilder class to help build query strings for Bookwhen API requests.
|
|
4
|
+
*/
|
|
5
|
+
export declare class BookwhenRequest {
|
|
6
|
+
private path;
|
|
7
|
+
private filters;
|
|
8
|
+
private includes;
|
|
9
|
+
constructor(path: string);
|
|
10
|
+
/**
|
|
11
|
+
* Adds filters to the BookwhenQuery object.
|
|
12
|
+
* @param filters - An array of key-value pairs representing the filters to be added.
|
|
13
|
+
*
|
|
14
|
+
* Example resultant query strings:
|
|
15
|
+
* /v2/events?filter[from]=20200401&filter[to]=20200404
|
|
16
|
+
* /v2/events?filter[tag]=tag%20one,tag%20two
|
|
17
|
+
* /v2/events?filter[title]=advanced,pro&filter[detail]=masterclass
|
|
18
|
+
*
|
|
19
|
+
* @see https://petstore.swagger.io/?url=https://api.bookwhen.com/v2/openapi.yaml
|
|
20
|
+
*
|
|
21
|
+
* @returns The updated BookwhenQuery object.
|
|
22
|
+
*/
|
|
23
|
+
addFilters(filters: Filters): BookwhenRequest;
|
|
24
|
+
/**
|
|
25
|
+
* Adds an include query parameter to the BookwhenRequest object.
|
|
26
|
+
* @param includes - An array of strings representing the resources to include.
|
|
27
|
+
*
|
|
28
|
+
* @returns The updated BookwhenRequest object.
|
|
29
|
+
*
|
|
30
|
+
* @throws Error if any include is not a valid string.
|
|
31
|
+
*/
|
|
32
|
+
addIncludes(includes: Resources): BookwhenRequest;
|
|
33
|
+
/**
|
|
34
|
+
* Does the actual work of putting together a valid query string given:
|
|
35
|
+
* - filters
|
|
36
|
+
* - includes
|
|
37
|
+
* - segments
|
|
38
|
+
*
|
|
39
|
+
* @returns
|
|
40
|
+
*/
|
|
41
|
+
build(): string;
|
|
42
|
+
/**
|
|
43
|
+
* @returns the constructed query string
|
|
44
|
+
*/
|
|
45
|
+
toString(): string;
|
|
46
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import z from 'zod';
|
|
2
|
+
/**
|
|
3
|
+
* QueryBuilder class to help build query strings for Bookwhen API requests.
|
|
4
|
+
*/
|
|
5
|
+
export class BookwhenRequest {
|
|
6
|
+
path = '';
|
|
7
|
+
filters = [];
|
|
8
|
+
includes = [];
|
|
9
|
+
constructor(path) {
|
|
10
|
+
try {
|
|
11
|
+
if (!path) {
|
|
12
|
+
throw new Error('Path is required');
|
|
13
|
+
}
|
|
14
|
+
this.path = path;
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
throw new Error("BookwhenQuery error: " + error.message);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Adds filters to the BookwhenQuery object.
|
|
22
|
+
* @param filters - An array of key-value pairs representing the filters to be added.
|
|
23
|
+
*
|
|
24
|
+
* Example resultant query strings:
|
|
25
|
+
* /v2/events?filter[from]=20200401&filter[to]=20200404
|
|
26
|
+
* /v2/events?filter[tag]=tag%20one,tag%20two
|
|
27
|
+
* /v2/events?filter[title]=advanced,pro&filter[detail]=masterclass
|
|
28
|
+
*
|
|
29
|
+
* @see https://petstore.swagger.io/?url=https://api.bookwhen.com/v2/openapi.yaml
|
|
30
|
+
*
|
|
31
|
+
* @returns The updated BookwhenQuery object.
|
|
32
|
+
*/
|
|
33
|
+
addFilters(filters) {
|
|
34
|
+
// @todo add zod validation for filters?
|
|
35
|
+
try {
|
|
36
|
+
for (let key in filters) {
|
|
37
|
+
let value = filters[key];
|
|
38
|
+
if (Array.isArray(value) && value && value.length > 0) {
|
|
39
|
+
// const encodedValues = value.map((v: string) => {
|
|
40
|
+
// console.log('typeof v', typeof v);
|
|
41
|
+
// if (typeof v === 'undefined') {
|
|
42
|
+
// return '';
|
|
43
|
+
// }
|
|
44
|
+
// if (v.length === 0) {
|
|
45
|
+
// return '';
|
|
46
|
+
// }
|
|
47
|
+
// return encodeURIComponent(v);
|
|
48
|
+
// }).join(',');
|
|
49
|
+
const encodedValues = value
|
|
50
|
+
.filter((v) => typeof v === 'string' && v.length > 0)
|
|
51
|
+
.map((v) => encodeURIComponent(v))
|
|
52
|
+
.join(',');
|
|
53
|
+
this.filters.push(`filter[${encodeURIComponent(key)}]=${encodedValues}`);
|
|
54
|
+
}
|
|
55
|
+
else if (typeof value === 'string' && value !== '') {
|
|
56
|
+
if (key.length === 0) {
|
|
57
|
+
throw new Error('Invalid filter key' + key);
|
|
58
|
+
}
|
|
59
|
+
this.filters.push(`filter[${encodeURIComponent(key)}]=${encodeURIComponent(value)}`);
|
|
60
|
+
}
|
|
61
|
+
else if (typeof value === 'undefined' && value !== '') {
|
|
62
|
+
// ignore undefined values
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
throw new Error("BookwhenQuery error: " + error.message);
|
|
68
|
+
}
|
|
69
|
+
return this;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Adds an include query parameter to the BookwhenRequest object.
|
|
73
|
+
* @param includes - An array of strings representing the resources to include.
|
|
74
|
+
*
|
|
75
|
+
* @returns The updated BookwhenRequest object.
|
|
76
|
+
*
|
|
77
|
+
* @throws Error if any include is not a valid string.
|
|
78
|
+
*/
|
|
79
|
+
addIncludes(includes) {
|
|
80
|
+
try {
|
|
81
|
+
const validIncludes = includes.filter((include) => include.length > 0);
|
|
82
|
+
if (validIncludes.length !== includes.length) {
|
|
83
|
+
throw new Error('Invalid includes');
|
|
84
|
+
}
|
|
85
|
+
if (validIncludes.length > 0) {
|
|
86
|
+
const includeQuery = validIncludes.join(',');
|
|
87
|
+
this.includes.push(`include=${includeQuery}`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
catch (error) {
|
|
91
|
+
throw new Error("BookwhenQuery error: " + error.message);
|
|
92
|
+
}
|
|
93
|
+
return this;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Does the actual work of putting together a valid query string given:
|
|
97
|
+
* - filters
|
|
98
|
+
* - includes
|
|
99
|
+
* - segments
|
|
100
|
+
*
|
|
101
|
+
* @returns
|
|
102
|
+
*/
|
|
103
|
+
build() {
|
|
104
|
+
let queryString = '';
|
|
105
|
+
// Concatenate filters and includes, if any
|
|
106
|
+
if (this.filters.length > 0) {
|
|
107
|
+
queryString += this.filters.join('&');
|
|
108
|
+
}
|
|
109
|
+
if (this.includes.length > 0) {
|
|
110
|
+
// Add '&' if filters already added to queryString
|
|
111
|
+
queryString += queryString ? '&' : '';
|
|
112
|
+
queryString += this.includes.join('&');
|
|
113
|
+
}
|
|
114
|
+
// Strip any leading slashes from this.path
|
|
115
|
+
const cleanedPath = this.path.replace(/^\/+/, '');
|
|
116
|
+
// Return the full URI, including query string only if it's non-empty
|
|
117
|
+
return `/${cleanedPath}${queryString ? '?' + queryString : ''}`;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* @returns the constructed query string
|
|
121
|
+
*/
|
|
122
|
+
toString() {
|
|
123
|
+
return this.build();
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
//# sourceMappingURL=BookwhenRequest.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BookwhenRequest.js","sourceRoot":"","sources":["../../src/request/BookwhenRequest.ts"],"names":[],"mappings":"AACA,OAAO,CAAC,MAAM,KAAK,CAAC;AAEpB;;GAEG;AACH,MAAM,OAAO,eAAe;IAElB,IAAI,GAAW,EAAE,CAAC;IAClB,OAAO,GAAa,EAAE,CAAC;IACvB,QAAQ,GAAa,EAAE,CAAC;IAEhC,YAAY,IAAY;QACtB,IAAI,CAAC;YACH,IAAI,CAAC,IAAI,EAAE,CAAC;gBACV,MAAM,IAAI,KAAK,CAAC,kBAAkB,CAAC,CAAC;YACtC,CAAC;YACD,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACnB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,IAAI,KAAK,CAAC,uBAAuB,GAAI,KAAe,CAAC,OAAO,CAAC,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