Skip to content

Commit

Permalink
Merge pull request #19 from Capster/allow-optional
Browse files Browse the repository at this point in the history
Allow ClientOptions to be completely optional
  • Loading branch information
Capster committed Jan 5, 2024
2 parents c181f5e + 9e3d9a6 commit de1779a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ To use the library, simply import it into your project and create an instance of
```typescript
import { client } from 'node-shikimori';

const shikimori = client({});
const shikimori = client();

const result = await shikimori.animes.byId({
id: 1
Expand Down
23 changes: 12 additions & 11 deletions src/apiProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

/**
Expand All @@ -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<any> => {
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) {
Expand Down
2 changes: 1 addition & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit de1779a

Please sign in to comment.