From b75eed86eceb87907b1d97ed955bbd51c56d4573 Mon Sep 17 00:00:00 2001 From: Mehdi Baaboura Date: Sat, 4 Nov 2023 23:26:43 +0100 Subject: [PATCH] Add new FastCache client --- src/client/index.ts | 9 +++ src/modules/fastcache/index.test.ts | 52 +++++++++++++++++ src/modules/fastcache/index.ts | 87 +++++++++++++++++++++++++++++ src/modules/mcskinhistory/index.ts | 0 src/modules/vat/index.ts | 0 src/modules/web-reputation/index.ts | 0 src/modules/whois/index.ts | 0 src/tests/mock.ts | 20 +++++++ 8 files changed, 168 insertions(+) create mode 100644 src/modules/fastcache/index.test.ts create mode 100644 src/modules/fastcache/index.ts create mode 100644 src/modules/mcskinhistory/index.ts create mode 100644 src/modules/vat/index.ts create mode 100644 src/modules/web-reputation/index.ts create mode 100644 src/modules/whois/index.ts create mode 100644 src/tests/mock.ts diff --git a/src/client/index.ts b/src/client/index.ts index 0a46079..9e4ce47 100644 --- a/src/client/index.ts +++ b/src/client/index.ts @@ -47,6 +47,15 @@ export class HttpClient { } } + protected async post(path: string, data: unknown, options: BaseRequestOptions = {}): Promise { + const headers = new Headers(options.headers); + headers.append('Content-Type', 'application/json'); + + options.headers = headers; + + return await this.request(path, 'POST', { ...options, body: JSON.stringify(data) }); + } + async handleError(response: Response): Promise { const responseText = await response.text(); diff --git a/src/modules/fastcache/index.test.ts b/src/modules/fastcache/index.test.ts new file mode 100644 index 0000000..4e8c174 --- /dev/null +++ b/src/modules/fastcache/index.test.ts @@ -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'); + }); +}); diff --git a/src/modules/fastcache/index.ts b/src/modules/fastcache/index.ts new file mode 100644 index 0000000..fe01678 --- /dev/null +++ b/src/modules/fastcache/index.ts @@ -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 { + 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 { + 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 { + 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; +} diff --git a/src/modules/mcskinhistory/index.ts b/src/modules/mcskinhistory/index.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/modules/vat/index.ts b/src/modules/vat/index.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/modules/web-reputation/index.ts b/src/modules/web-reputation/index.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/modules/whois/index.ts b/src/modules/whois/index.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/tests/mock.ts b/src/tests/mock.ts new file mode 100644 index 0000000..d7f4fbb --- /dev/null +++ b/src/tests/mock.ts @@ -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 + ); +}