diff --git a/src/apiProvider.ts b/src/apiProvider.ts index 7c78855..9f20b7e 100644 --- a/src/apiProvider.ts +++ b/src/apiProvider.ts @@ -13,17 +13,17 @@ export interface ClientOptions { * Represents a user agent used to send requests * @defaultValue 'node-shikimori' */ - clientName: string, + clientName?: string, /** * A number representing the maximum number of API calls allowed per second * @defaultValue 5 */ - maxCallsPerSecond: number, + maxCallsPerSecond?: number, /** * A number representing the maximum number of API calls allowed per minute * @defaultValue 90 */ - maxCallsPerMinute: number, + maxCallsPerMinute?: number, } /** @@ -50,20 +50,21 @@ export const httpMethods = (request: RequestHandler): RequestMethods => ({ _delete: request.bind(null, 'DELETE'), }); -export const apiProvider = (options: ClientOptions = { - clientName: DEFAULT_USER_AGENT, - maxCallsPerSecond: MAX_CALLS_PER_SECOND, - maxCallsPerMinute: MAX_CALLS_PER_MINUTE, -}): [RequestHandler, (token: Token) => void] => { - const request = limitedRequest(options.maxCallsPerSecond, options.maxCallsPerMinute); - let accessToken: Token = options.token; +export const apiProvider = ({ + token, + clientName = DEFAULT_USER_AGENT, + maxCallsPerSecond = MAX_CALLS_PER_SECOND, + maxCallsPerMinute = MAX_CALLS_PER_MINUTE, +}: ClientOptions): [RequestHandler, (token: Token) => void] => { + const request = limitedRequest(maxCallsPerSecond, maxCallsPerMinute); + let accessToken: Token = token; const apiRequest: RequestHandler = async (type, endpoint, params): Promise => { const searchParams = new URLSearchParams(Object.entries(params)); const fullPath = `/api${endpoint}?${searchParams.toString()}`; const headers = new Headers({ - 'User-Agent': options.clientName, + 'User-Agent': clientName, }); if (accessToken) { diff --git a/src/client.ts b/src/client.ts index 521da5c..5f7a79e 100644 --- a/src/client.ts +++ b/src/client.ts @@ -38,7 +38,7 @@ import { * @param options - The options to configure the client, including the base URL, OAuth2 credentials, etc. * @returns An object containing methods for interacting with various endpoints, as well as a function for setting the access token */ -export const client = (options: ClientOptions) => { +export const client = (options: ClientOptions = {}) => { const [request, setAccessToken] = apiProvider(options); const methods = httpMethods(request);