Skip to content

Commit c20e490

Browse files
committed
examples, parse param in httpraw, fix put
1 parent dcd98dd commit c20e490

File tree

4 files changed

+85
-15
lines changed

4 files changed

+85
-15
lines changed

README.md

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ ws.on('error', (_) => _);
5757
ws.on<[string, string, number]>(
5858
'message',
5959
({ data }) =>
60-
console.log(`${data[0]} said "${data[1]}" and sent ${data[2]} attachments`),
60+
console.log(`${data[0]} said "${data[1]}" and sent ${data[2]} attachment(s).`),
6161
);
6262

6363
// Remember this!
@@ -79,20 +79,76 @@ discord.on('MESSAGE_CREATE', async (message) => {
7979
});
8080
```
8181

82-
<!--
82+
#### Get a guild's statistics.
8383

84-
const client = new Tpy(Deno.env.get('PYLON_TOKEN')!);
85-
const guildStats = await client.getGuildStats('759174794968301569');
84+
```ts
85+
const client = new Tpy('PYLON_TOKEN');
86+
const guildStats = await client.getGuildStats('GUILD_ID');
8687
const mostRecent = guildStats.find((e) =>
8788
e.date === Math.min(...guildStats.map((e) => e.date))
89+
)!;
90+
const { date, events, executionMsAvg } = mostRecent;
91+
92+
const mostRecentDateFormatted = new Date(date * 1000).toDateString();
93+
console.log(
94+
`On ${mostRecentDateFormatted}, there was a total of ${events} events with an average execution time of ${executionMsAvg} (in ms).`,
95+
);
96+
```
97+
98+
#### Get a deployment's listening events and cron tasks.
99+
100+
```ts
101+
const client = new Tpy('PYLON_TOKEN');
102+
const { config } = await client.getDeploymentfromGuild('GUILD_ID');
103+
const { cronTasks } = config.tasks;
104+
const { events } = config;
105+
const cronTasksFormatted = cronTasks.map(({ cronString, name }) =>
106+
` ${name} (${cronString})`
107+
);
108+
109+
console.log(
110+
`Listening to ${events.length} discord event(s):
111+
${events.join(', ')}\n`,
112+
`Running ${cronTasks.length} cron job(s):\n${cronTasksFormatted.join('\n')}`,
113+
);
114+
```
115+
116+
#### Get the keys in a KV namespace.
117+
118+
```ts
119+
const client = new Tpy('PYLON_TOKEN');
120+
const kvnamespace = 'tags';
121+
const kv = client.KV(
122+
kvnamespace,
123+
(await client.getDeploymentfromGuild('GUILD_ID')).id,
88124
);
89-
if (!mostRecent) throw '???';
90-
const mostRecentDateFormatted = new Date(mostRecent.date).getMilliseconds();
91-
console.log(mostRecentDateFormatted);
92125

93-
-->
126+
const keys = await kv.list({ limit: 10 });
127+
const amountOfKeys = await kv.count();
128+
129+
console.log(
130+
`There are ${amountOfKeys} key(s) within the ${kvnamespace} KV namespace, these are the first 10 (or less).
131+
${keys.join(', ')}`,
132+
);
133+
```
94134

95-
<!-- TODO: add more examples; ws, kv, post deployment, other get stuff -->
135+
#### Get and modify values within a KV namespace.
136+
137+
```ts
138+
const client = new Tpy('PYLON_TOKEN');
139+
const kvnamespace = 'tags';
140+
const kv = client.KV(
141+
kvnamespace,
142+
(await client.getDeploymentfromGuild('GUILD_ID')).id,
143+
);
144+
145+
const key = 'cool';
146+
console.log(`Value of key "${key}":`, await kv.get(key));
147+
await kv.put(key, 'rust');
148+
console.log(`Value of key "${key}":`, await kv.get(key));
149+
await kv.delete(key);
150+
console.log(`Value of key "${key}":`, await kv.get(key));
151+
```
96152

97153
## Contributing
98154

TODO

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
do mod.ts again
2+
examples

src/kv.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,12 @@ export default class TpyKV {
7474
new Context({ deploymentID }),
7575
`/deployments/${deploymentID}/kv/namespaces/${kvnamespace}/items/${key}`,
7676
'PUT',
77-
{ body: JSON.stringify({ 'string': value }) },
77+
{
78+
body: JSON.stringify({
79+
'string': typeof value === 'string' ? `"${value}"` : value,
80+
}),
81+
},
82+
false,
7883
);
7984
}
8085

@@ -125,7 +130,9 @@ export default class TpyKV {
125130
response,
126131
);
127132
}
128-
item = JSON.parse(p.value.string);
133+
item = ['\'', '"', '`'].includes(p.value.string[0])
134+
? JSON.parse(p.value.string)
135+
: p.value.string;
129136
break;
130137
}
131138
return item;

src/tpy.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,22 +263,27 @@ export default class Tpy {
263263
*
264264
* @param resource The resource to request that will be concatenated with the API URL.
265265
* @param method HTTP method to use. Currently, the Pylon API only uses GET and POST.
266-
* @param other Other fetch parameters.
266+
* @param requestInit Other fetch parameters.
267+
* @param parse Whether to parse out the body or not, default is true.
267268
*
268269
* @throws {TpyError<Response | Context>}
269270
*/
270271
async httpRaw<T>(
271272
ctx: Context,
272273
resource: `/${string}`,
273274
method: Pylon.HTTPVerbs = 'GET',
274-
other: RequestInit = {},
275+
requestInit: RequestInit = {},
276+
parse = true,
275277
): Promise<T> {
276278
const response = await fetch(
277279
'https://pylon.bot/api' + resource,
278-
this.readyRequest(method, other),
280+
this.readyRequest(method, requestInit),
279281
);
280282

281-
if (response.ok) return await response.json() as T;
283+
if (response.ok) {
284+
if (parse) return await response.json() as T;
285+
return {} as T;
286+
}
282287

283288
switch (response.status) {
284289
case 404: {

0 commit comments

Comments
 (0)