Skip to content

Commit

Permalink
Add new FastCache client
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeryther committed Nov 4, 2023
1 parent 02947c0 commit b75eed8
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ export class HttpClient {
}
}

protected async post<T>(path: string, data: unknown, options: BaseRequestOptions = {}): Promise<T> {
const headers = new Headers(options.headers);
headers.append('Content-Type', 'application/json');

options.headers = headers;

return await this.request<T>(path, 'POST', { ...options, body: JSON.stringify(data) });
}

async handleError(response: Response): Promise<void> {
const responseText = await response.text();

Expand Down
52 changes: 52 additions & 0 deletions src/modules/fastcache/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { mockFetch, mockFetchError } from '../../tests/mock';
import fastcache from './index';

describe('FastCache', () => {
it('should be able to get a FastCache item', async () => {
mockFetch({
key: 'key',
value: 'value',
expiration: 1234567890,
byteSize: 6,
});

const item = await fastcache.get('key');

expect(item).not.toBeNull();

expect(item?.key).toBe('key');
expect(item?.value).toBe('value');
expect(item?.expiration).toBe(1234567890);
expect(item?.byteSize).toBe(6);
});

it('should return null if the item does not exist', async () => {
mockFetchError('Not found');

const item = await fastcache.get('key');

expect(item).toBeNull();
});

it('should be able to set a FastCache item', async () => {
mockFetch({
key: 'key',
value: 'value',
expiration: 1234567890,
byteSize: 6,
});

const item = await fastcache.set('key', 'value', 1234567890);

expect(item.key).toBe('key');
expect(item.value).toBe('value');
expect(item.expiration).toBe(1234567890);
expect(item.byteSize).toBe(6);
});

it('should be able to delete a FastCache item', async () => {
mockFetch();

await fastcache.delete('key');
});
});
87 changes: 87 additions & 0 deletions src/modules/fastcache/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { type BaseRequestOptions, HttpClient } from '../../client';

/**
* FastCache is a key-value store that is optimized for speed and low latency by being hosted at the edge.
*
* This client allows you to interact with the FastCache API.
*
* @see https://docs.gigadrive.network/products/fastcache
*/
export class FastCacheClient extends HttpClient {
/**
* @param baseURL The base URL of the API. Defaults to `https://api.gigadrive.network`.
*/
constructor(baseURL: string = 'https://api.gigadrive.network') {
super(baseURL);
}

/**
* Retrieves a FastCache item by key.
*
* Required API Key permission: `fastcache:read`
*
* @param key The key of the FastCache item
* @param options The request options
* @returns The FastCache item. Returns null if the item does not exist.
* @see https://docs.gigadrive.network/products/fastcache#retrieve-an-item
*/
async get(key: string, options: BaseRequestOptions = {}): Promise<FastCacheItem | null> {
return await this.requestNullable(`/fastcache?key=${key}`, 'GET', options);
}

/**
* Saves an item to FastCache.
*
* Required API Key permission: `fastcache:write`
*
* @param key The key of the FastCache item
* @param value The value of the FastCache item
* @param expiration The unix timestamp when the item should expire. Set to null to never expire.
* @param options The request options
* @returns The FastCache item
* @see https://docs.gigadrive.network/products/fastcache#create-an-item
*/
async set(key: string, value: string, expiration?: number, options: BaseRequestOptions = {}): Promise<FastCacheItem> {
return await this.post('/fastcache', { key, value, expiration }, options);
}

/**
* Deletes a FastCache item by key.
*
* Required API Key permission: `fastcache:delete`
*
* @param key The key of the FastCache item
* @param options The request options
* @see https://docs.gigadrive.network/products/fastcache#delete-an-item
*/
async delete(key: string, options: BaseRequestOptions = {}): Promise<void> {
await this.request(`/fastcache?key=${key}`, 'DELETE', options);
}
}

export default new FastCacheClient();

/**
* Represents an item saved to FastCache.
*/
export interface FastCacheItem {
/**
* The main identifier of the item.
*/
key: string;

/**
* The value of the item.
*/
value: string;

/**
* The unix timestamp when the item expires.
*/
expiration: number | null;

/**
* The size of the item in bytes.
*/
byteSize: number;
}
Empty file.
Empty file added src/modules/vat/index.ts
Empty file.
Empty file.
Empty file added src/modules/whois/index.ts
Empty file.
20 changes: 20 additions & 0 deletions src/tests/mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export function mockFetch(result: unknown = undefined, ok: boolean = true) {
global.fetch = jest.fn().mockResolvedValue({
ok,
json: () => result,
text: () => JSON.stringify(result),
});
}

export function mockFetchError(message: string) {
mockFetch(
{
errors: [
{
message,
},
],
},
false
);
}

0 comments on commit b75eed8

Please sign in to comment.