Skip to content

Commit

Permalink
Fixed httpraw parse, guild stat date docs (ty y21)
Browse files Browse the repository at this point in the history
  • Loading branch information
insyri committed Oct 19, 2022
1 parent c20e490 commit cd1f2c4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ following qualities:
- 📄 Extensive documentation.
- 📞 Keep-alive WebSocket client.

## Documentation

The API documentation can be viewed on the
[Deno website](https://deno.land/x/tpy/mod.ts/).

Expand All @@ -38,9 +40,9 @@ download the type dependencies locally.

```ts
const client = new Tpy('PYLON_TOKEN');
const user = await client.getUser();
const { displayName, id } = await client.getUser();

console.log(`Logged in as ${user.displayName} (<@${user.id}>).`);
console.log(`Logged in as ${displayName} (<@${id}>).`);
```

#### Listen to a deployment's console output.
Expand Down Expand Up @@ -142,10 +144,13 @@ const kv = client.KV(
(await client.getDeploymentfromGuild('GUILD_ID')).id,
);

const key = 'cool';
const key = '';

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));
```
Expand Down
3 changes: 3 additions & 0 deletions src/kv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export default class TpyKV {
`/deployments/${deploymentID}/kv/namespaces/${kvnamespace}/items/${key}`,
'PUT',
{ body: JSON.stringify({ 'bytes': value }) },
false,
);
}

Expand Down Expand Up @@ -273,6 +274,8 @@ export default class TpyKV {
new Context({ deploymentID, kvnamespace }),
`/deployments/${deploymentID}/kv/namespaces/${kvnamespace}/items/${key}`,
'DELETE',
{},
false,
);

if (!options?.prevValue) await del();
Expand Down
16 changes: 11 additions & 5 deletions src/tpy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,23 +266,29 @@ export default class Tpy {
* @param requestInit Other fetch parameters.
* @param parse Whether to parse out the body or not, default is true.
*
* @template T The return type of the response body.
* @template Parse Follows the {@linkcode parse} parameter and should match values. Determines whether T is returned or not.
*
* @throws {TpyError<Response | Context>}
*/
async httpRaw<T>(
async httpRaw<T, Parse extends boolean = true>(
ctx: Context,
resource: `/${string}`,
method: Pylon.HTTPVerbs = 'GET',
requestInit: RequestInit = {},
parse = true,
): Promise<T> {
parse: Parse = (true as Parse),
): Promise<Parse extends true ? T : void> {
const response = await fetch(
'https://pylon.bot/api' + resource,
this.readyRequest(method, requestInit),
);

if (response.ok) {
if (parse) return await response.json() as T;
return {} as T;
return (
parse
? await response.json() as Parse extends true ? T : void
: undefined as Parse extends true ? T : void
);
}

switch (response.status) {
Expand Down
2 changes: 1 addition & 1 deletion src/types/guild.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ declare namespace Guild {
*/
export type Stats = Array<{
/**
* Date (Unix timestamp) of when statistics were captured.
* Date (Unix timestamp) in seconds of when statistics were captured.
*/
date: number;
/**
Expand Down

0 comments on commit cd1f2c4

Please sign in to comment.