forked from benmvp/eventbrite-sdk-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
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 #1 from jcreamer898/jc-add-users
swap functions for classes
- Loading branch information
Showing
7 changed files
with
141 additions
and
91 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 |
---|---|---|
|
@@ -10,11 +10,15 @@ import { | |
} from './__fixtures__'; | ||
|
||
import request from '../request'; | ||
import usersMethods from '../users'; | ||
|
||
const users = usersMethods(request); | ||
import {UserApi} from '../users'; | ||
|
||
describe('users.me()', () => { | ||
let users: UserApi; | ||
|
||
beforeEach(() => { | ||
users = new UserApi(request); | ||
}); | ||
|
||
it('calls fetch and calls fetch with appropriate defaults', async() => { | ||
mockFetch(getMockResponse(MOCK_USERS_ME_RESPONSE_DATA)); | ||
|
||
|
@@ -61,6 +65,12 @@ describe('users.me()', () => { | |
}); | ||
|
||
describe('users.get(id)', () => { | ||
let users: UserApi; | ||
|
||
beforeEach(() => { | ||
users = new UserApi(request); | ||
}); | ||
|
||
it('calls fetch and calls fetch with appropriate defaults', async() => { | ||
mockFetch(getMockResponse(MOCK_USERS_ME_RESPONSE_DATA)); | ||
|
||
|
@@ -106,6 +116,12 @@ describe('users.get(id)', () => { | |
}); | ||
|
||
describe('users.emailLookup(email)', () => { | ||
let users: UserApi; | ||
|
||
beforeEach(() => { | ||
users = new UserApi(request); | ||
}); | ||
|
||
it('calls fetch and calls fetch with appropriate defaults', async() => { | ||
mockFetch(getMockResponse(MOCK_USERS_ME_RESPONSE_DATA)); | ||
const email = '[email protected]'; | ||
|
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,54 @@ | ||
import {JSONRequest} from './types'; | ||
|
||
const SNAKE_CASE_MATCH = /_\w/g; | ||
const snakeToCamel = (str: string) => | ||
str.replace(SNAKE_CASE_MATCH, (chars: string) => chars[1].toUpperCase()); | ||
|
||
const transformKeysSnakeToCamel = <T extends { [key: string]: any } = {}>( | ||
obj: T | ||
) => | ||
Object.keys(obj).reduce((memo, key) => { | ||
let newValue = obj[key]; | ||
const camelKey = snakeToCamel(key); | ||
|
||
if ( | ||
newValue && | ||
typeof newValue === 'object' && | ||
!Array.isArray(newValue) | ||
) { | ||
newValue = transformKeysSnakeToCamel(newValue); | ||
} | ||
|
||
return { | ||
...memo, | ||
[camelKey]: newValue, | ||
}; | ||
}, {}) as T; | ||
|
||
/** | ||
* Returns a function that sends a request, and transforms its results | ||
*/ | ||
const makeJsonRequest = <T>( | ||
request: JSONRequest<T>, | ||
transformers: Array<(obj: T) => T> | ||
) => (url: string, options?: RequestInit) => | ||
request(url, options).then((response) => | ||
transformers.reduce<T>((acc, transformer) => { | ||
let memo = acc; | ||
|
||
memo = transformer(response); | ||
return memo; | ||
}, response) | ||
); | ||
|
||
/** | ||
* Base API class for creating new API Classes. | ||
* Also encapsulates default transformers such as snake to camel. | ||
*/ | ||
export abstract class BaseApi<T> { | ||
request: JSONRequest<T>; | ||
|
||
constructor(req: JSONRequest<T>) { | ||
this.request = makeJsonRequest(req, [transformKeysSnakeToCamel]); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,36 +1,42 @@ | ||
import {Sdk, SdkConfig, JSONRequest} from './types'; | ||
import request from './request'; | ||
import userMethods from './users'; | ||
import {UserApi} from './users'; | ||
|
||
export * from './constants'; | ||
|
||
const DEFAULT_API_URL = 'https://www.eventbriteapi.com/v3'; | ||
|
||
type MakeRequestFunction = <T>( | ||
baseUrl: string, | ||
token: string | ||
) => JSONRequest<T>; | ||
|
||
const makeRequest: MakeRequestFunction = (baseUrl: string, token: string) => ( | ||
endpoint, | ||
options = {} | ||
) => { | ||
const url = `${baseUrl}${endpoint}`; | ||
let requestOptions = options; | ||
|
||
if (token) { | ||
requestOptions = { | ||
...requestOptions, | ||
headers: { | ||
...(requestOptions.headers || {}), | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
}; | ||
} | ||
|
||
return request(url, requestOptions); | ||
}; | ||
|
||
const eventbrite = ({ | ||
baseUrl = DEFAULT_API_URL, | ||
token, | ||
}: SdkConfig = {}): Sdk => { | ||
const requestHelper: JSONRequest = (endpoint, options = {}) => { | ||
const url = `${baseUrl}${endpoint}`; | ||
let requestOptions = options; | ||
|
||
if (token) { | ||
requestOptions = { | ||
...requestOptions, | ||
headers: { | ||
...(requestOptions.headers || {}), | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
}; | ||
} | ||
|
||
return request(url, requestOptions); | ||
}; | ||
|
||
return { | ||
request: requestHelper, | ||
users: userMethods(requestHelper), | ||
}; | ||
}; | ||
}: SdkConfig = {}): Sdk => ({ | ||
request: makeRequest(baseUrl, token), | ||
users: new UserApi(makeRequest(baseUrl, token)), | ||
}); | ||
|
||
export default eventbrite; |
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
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 |
---|---|---|
@@ -1,66 +1,40 @@ | ||
import {JSONRequest} from './types'; | ||
import {BaseApi} from './baseApi'; | ||
|
||
export type Email = { | ||
email: string; | ||
primary: boolean; | ||
}; | ||
|
||
export interface User { | ||
id: string; | ||
firstName: string; | ||
lastName: string; | ||
imageId: string; | ||
email: Email[]; | ||
export interface Email { | ||
email?: string; | ||
primary?: boolean; | ||
} | ||
|
||
export interface UserMethods { | ||
[key: string]: () => Promise<User>; | ||
me: () => Promise<User>; | ||
get: (id: string) => Promise<User>; | ||
emailLookup: (email: string) => Promise<User>; | ||
export interface User { | ||
id?: string; | ||
firstName?: string; | ||
lastName?: string; | ||
imageId?: string; | ||
email?: Email[]; | ||
} | ||
|
||
const SNAKE_CASE_MATCH = /_\w/g; | ||
const snakeToCamel = (str: string) => | ||
str.replace(SNAKE_CASE_MATCH, (chars: string) => chars[1].toUpperCase()); | ||
/** | ||
* API for working with Users | ||
*/ | ||
export class UserApi extends BaseApi<User> { | ||
async me() { | ||
const response = await this.request('/users/me/'); | ||
|
||
const transformKeysSnakeToCamel = (obj: { [key: string]: any }): {} => | ||
Object.keys(obj).reduce((memo, key) => { | ||
let newValue = obj[key]; | ||
const camelKey = snakeToCamel(key); | ||
return response; | ||
} | ||
|
||
if ( | ||
newValue && | ||
typeof newValue === 'object' && | ||
!Array.isArray(newValue) | ||
) { | ||
newValue = transformKeysSnakeToCamel(newValue); | ||
} | ||
async get(id: string) { | ||
const response = await this.request(`/users/${id}/`); | ||
|
||
return { | ||
...memo, | ||
[camelKey]: newValue, | ||
}; | ||
}, {}); | ||
return response; | ||
} | ||
|
||
export default (request: JSONRequest): UserMethods => { | ||
const me = () => | ||
request('/users/me/').then(transformKeysSnakeToCamel) as Promise<User>; | ||
|
||
const get = (id: string) => | ||
request(`/users/${id}/`).then(transformKeysSnakeToCamel) as Promise< | ||
User | ||
>; | ||
|
||
const emailLookup = (email: string) => | ||
request('/users/lookup/', { | ||
async emailLookup(email: string) { | ||
const response = await this.request('/users/lookup/', { | ||
method: 'POST', | ||
body: JSON.stringify({email}), | ||
}).then(transformKeysSnakeToCamel) as Promise<User>; | ||
}); | ||
|
||
return { | ||
me, | ||
get, | ||
emailLookup, | ||
}; | ||
}; | ||
return response; | ||
} | ||
} |