-
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.
- Loading branch information
Showing
8 changed files
with
168 additions
and
0 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 |
---|---|---|
@@ -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'); | ||
}); | ||
}); |
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,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.
Empty file.
Empty file.
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,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 | ||
); | ||
} |