diff --git a/README.md b/README.md index d43aca4..83b15ec 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ ws.on('error', (_) => _); ws.on<[string, string, number]>( 'message', ({ data }) => - console.log(`${data[0]} said "${data[1]}" and sent ${data[2]} attachments`), + console.log(`${data[0]} said "${data[1]}" and sent ${data[2]} attachment(s).`), ); // Remember this! @@ -79,20 +79,76 @@ discord.on('MESSAGE_CREATE', async (message) => { }); ``` - +const keys = await kv.list({ limit: 10 }); +const amountOfKeys = await kv.count(); + +console.log( + `There are ${amountOfKeys} key(s) within the ${kvnamespace} KV namespace, these are the first 10 (or less). + ${keys.join(', ')}`, +); +``` - +#### Get and modify values within a KV namespace. + +```ts +const client = new Tpy('PYLON_TOKEN'); +const kvnamespace = 'tags'; +const kv = client.KV( + kvnamespace, + (await client.getDeploymentfromGuild('GUILD_ID')).id, +); + +const key = 'cool'; +console.log(`Value of key "${key}":`, await kv.get(key)); +await kv.put(key, 'rust'); +console.log(`Value of key "${key}":`, await kv.get(key)); +await kv.delete(key); +console.log(`Value of key "${key}":`, await kv.get(key)); +``` ## Contributing diff --git a/TODO b/TODO new file mode 100644 index 0000000..12068af --- /dev/null +++ b/TODO @@ -0,0 +1,2 @@ +do mod.ts again +examples \ No newline at end of file diff --git a/src/kv.ts b/src/kv.ts index 34c7c64..5fab8d6 100644 --- a/src/kv.ts +++ b/src/kv.ts @@ -74,7 +74,12 @@ export default class TpyKV { new Context({ deploymentID }), `/deployments/${deploymentID}/kv/namespaces/${kvnamespace}/items/${key}`, 'PUT', - { body: JSON.stringify({ 'string': value }) }, + { + body: JSON.stringify({ + 'string': typeof value === 'string' ? `"${value}"` : value, + }), + }, + false, ); } @@ -125,7 +130,9 @@ export default class TpyKV { response, ); } - item = JSON.parse(p.value.string); + item = ['\'', '"', '`'].includes(p.value.string[0]) + ? JSON.parse(p.value.string) + : p.value.string; break; } return item; diff --git a/src/tpy.ts b/src/tpy.ts index 495fadf..e647300 100644 --- a/src/tpy.ts +++ b/src/tpy.ts @@ -263,7 +263,8 @@ export default class Tpy { * * @param resource The resource to request that will be concatenated with the API URL. * @param method HTTP method to use. Currently, the Pylon API only uses GET and POST. - * @param other Other fetch parameters. + * @param requestInit Other fetch parameters. + * @param parse Whether to parse out the body or not, default is true. * * @throws {TpyError} */ @@ -271,14 +272,18 @@ export default class Tpy { ctx: Context, resource: `/${string}`, method: Pylon.HTTPVerbs = 'GET', - other: RequestInit = {}, + requestInit: RequestInit = {}, + parse = true, ): Promise { const response = await fetch( 'https://pylon.bot/api' + resource, - this.readyRequest(method, other), + this.readyRequest(method, requestInit), ); - if (response.ok) return await response.json() as T; + if (response.ok) { + if (parse) return await response.json() as T; + return {} as T; + } switch (response.status) { case 404: {