Skip to content

Commit

Permalink
Run fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
FinlayDaG33k committed Sep 18, 2024
1 parent 473f8bf commit e006027
Show file tree
Hide file tree
Showing 47 changed files with 741 additions and 627 deletions.
25 changes: 18 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
# Chomp
Library of (arguably) useful stuff.
Should work just fine but comes with no warranties whatsoever.

Library of (arguably) useful stuff.\
Should work just fine but comes with no warranties whatsoever.

## Usage

Chomp is structured in such a way that you can import just what you need for your app.
Chomp is structured in such a way that you can import just what you need for your app.\
A good start would be to import the most common things you might use:

```ts
import * from "https://deno.land/x/chomp/common.ts";
```

This includes (list might not always be up-to-date):

- [Cache](docs/core/cache.md)
- [Configure](docs/core/configure.md)
- [Logger](docs/logging/logger.md)
Expand All @@ -21,25 +24,33 @@ This includes (list might not always be up-to-date):
You can then import any of the "extras" as you need:

- [Discord Bot](docs/discord/README.md) (Discordeno Wrapper):

```ts
import * from "https://deno.land/x/chomp/discord/mod.ts";
```

- [Webserver](docs/webserver/README.md):
```ts

```ts
import * from "https://deno.land/x/chomp/webserver/mod.ts";
```

- [Websocket Server](docs/websocket/README.md):
```ts

```ts
import * from "https://deno.land/x/chomp/websocket/mod.ts";
```

Additionally, you can explore the [docs](/docs) or [Deno.land](https://doc.deno.land/https://deno.land/x/chomp/mod.ts) to see what more Chomp is capable off!
Additionally, you can explore the [docs](/docs) or [Deno.land](https://doc.deno.land/https://deno.land/x/chomp/mod.ts)
to see what more Chomp is capable off!

**NOTE**: While you can import `https://deno.land/x/chomp/mod.ts`, we advice against this as it'll load the entire codebase, including stuff you may not actually be using.
**NOTE**: While you can import `https://deno.land/x/chomp/mod.ts`, we advice against this as it'll load the entire
codebase, including stuff you may not actually be using.

## Versioning

Versions adhere to the following versioning system of `x.y.z` where:

- `x` means a breaking change (eg. removal of a function, breaking upgrade of an upstream dependency etc.).
- `y` means an addition or non-breaking update.
- `z` means a typos, bug-fix etc.
59 changes: 29 additions & 30 deletions communication/couchdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ export interface CouchResponse {
status: number;
statusText: string;
// deno-lint-ignore no-explicit-any -- Any arbitrary data may be used
data: any|null;
error: null|{
data: any | null;
error: null | {
error: string;
reason: string;
};
Expand All @@ -19,10 +19,9 @@ interface CouchOverrides {
}

export class CouchDB {
private auth = '';
private auth = "";

/**
*
* @example
* ```ts
* import { CouchDB } from "https://deno.land/x/chomp/communication/couchdb.ts";
Expand All @@ -42,9 +41,9 @@ export class CouchDB {
* @param auth
*/
public constructor(
private readonly host: string = 'http://localhost:5984',
private readonly host: string = "http://localhost:5984",
private readonly database: string,
auth: Auth = {username: '', password: ''},
auth: Auth = { username: "", password: "" },
) {
this.auth = btoa(`${auth.username}:${auth.password}`);
}
Expand All @@ -65,7 +64,7 @@ export class CouchDB {
*/
public set username(username: string) {
// Get the password from the data
const password = atob(this.auth).split(':')[1];
const password = atob(this.auth).split(":")[1];

// Update auth string
this.auth = btoa(`${username}:${password}`);
Expand All @@ -87,7 +86,7 @@ export class CouchDB {
*/
public set password(password: string) {
// Get the password from the data
const username = atob(this.auth).split(':')[0];
const username = atob(this.auth).split(":")[0];

// Update auth string
this.auth = btoa(`${username}:${password}`);
Expand Down Expand Up @@ -139,7 +138,7 @@ export class CouchDB {
*/
// deno-lint-ignore no-explicit-any -- Any arbitrary data may be used
public async insert(data: any): Promise<CouchResponse> {
return await this.raw('', data);
return await this.raw("", data);
}

/**
Expand Down Expand Up @@ -167,10 +166,10 @@ export class CouchDB {
// deno-lint-ignore no-explicit-any -- Any arbitrary data may be used
public async update(id: string, revision: string, data: any): Promise<CouchResponse> {
// Make sure the id and revision are set in the data
if(!data['_id'] || data['_id'] !== id) data['_id'] = id;
if(!data['_rev'] || data['_rev'] !== revision) data['_rev'] = revision;
if (!data["_id"] || data["_id"] !== id) data["_id"] = id;
if (!data["_rev"] || data["_rev"] !== revision) data["_rev"] = revision;

return await this.raw(id, data, { method: 'PUT' });
return await this.raw(id, data, { method: "PUT" });
}

/**
Expand All @@ -197,14 +196,14 @@ export class CouchDB {
public async upsert(id: string, data: any): Promise<CouchResponse> {
// Check if a document already exists
// Insert a new document if not
const exists = await this.raw(id, null, { method: 'GET' });
if(exists.status === 404) {
data['_id'] = id;
const exists = await this.raw(id, null, { method: "GET" });
if (exists.status === 404) {
data["_id"] = id;
return await this.insert(data);
}

// Update the document
return await this.update(id, exists.data['_rev'], data);
return await this.update(id, exists.data["_rev"], data);
}

/**
Expand All @@ -228,8 +227,8 @@ export class CouchDB {
* @param id
* @param revision
*/
public async delete(id: string, revision: string): Promise <CouchResponse> {
return await this.raw(`${id}?rev=${revision}`, null, { method: 'DELETE' });
public async delete(id: string, revision: string): Promise<CouchResponse> {
return await this.raw(`${id}?rev=${revision}`, null, { method: "DELETE" });
}

/**
Expand All @@ -250,7 +249,7 @@ export class CouchDB {
* @param view
* @param partition
*/
public async viewDesign(design: string, view: string, partition: string): Promise<CouchResponse>{
public async viewDesign(design: string, view: string, partition: string): Promise<CouchResponse> {
return await this.raw(`_partition/${partition}/_design/${design}/_view/${view}`);
}

Expand All @@ -271,26 +270,26 @@ export class CouchDB {
public async raw(endpoint: string, body: any = null, overrides: CouchOverrides = {}): Promise<CouchResponse> {
// Start building opts
const opts = {
method: overrides['method'] ? overrides['method'] : 'GET',
method: overrides["method"] ? overrides["method"] : "GET",
headers: {
Authorization: `Basic ${this.auth}`,
}
},
};

// Add body if specified
if(body !== null) {
opts['method'] = opts.method !== 'GET' ? opts.method : 'POST';
opts['body'] = JSON.stringify(body);
opts.headers['Content-Type'] = 'application/json';
if (body !== null) {
opts["method"] = opts.method !== "GET" ? opts.method : "POST";
opts["body"] = JSON.stringify(body);
opts.headers["Content-Type"] = "application/json";
}

// Make sure the endpoint starts with a leading slash
if(endpoint.charAt(0) !== '/' && endpoint !== '') endpoint = `/${endpoint}`;
if (endpoint.charAt(0) !== "/" && endpoint !== "") endpoint = `/${endpoint}`;

// Send our request and get the response
const resp = await fetch(`${this.host}/${this.database}${endpoint}`, opts);
let data = null;
if(opts.method !== 'HEAD') data = await resp.json();
if (opts.method !== "HEAD") data = await resp.json();

// Prepare our CouchResponse
const couchResponse: CouchResponse = {
Expand All @@ -301,10 +300,10 @@ export class CouchDB {
};

// Check whether we have an error
if(resp.ok) {
couchResponse['data'] = data;
if (resp.ok) {
couchResponse["data"] = data;
} else {
couchResponse['error'] = data;
couchResponse["error"] = data;
}

return couchResponse;
Expand Down
16 changes: 10 additions & 6 deletions communication/druid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ export class Druid {
// deno-lint-ignore no-explicit-any -- TODO
private spec: any = null;
// deno-lint-ignore no-explicit-any -- TODO
public set setSpec(spec: any) { this.spec = spec; }
public set setSpec(spec: any) {
this.spec = spec;
}
// deno-lint-ignore no-explicit-any -- TODO
public get getSpec(): any { return this.spec; }
public get getSpec(): any {
return this.spec;
}

public constructor(
private readonly host: string,
Expand All @@ -17,13 +21,13 @@ export class Druid {
* @returns Promise<Response>
*/
public async create(): Promise<Response> {
if(!this.spec) throw Error('No task specification has been set!');
if (!this.spec) throw Error("No task specification has been set!");
return await fetch(`${this.host}/druid/indexer/v1/task`, {
method: 'POST',
method: "POST",
body: this.spec,
headers: {
'Content-Type': 'application/json'
}
"Content-Type": "application/json",
},
});
}
}
24 changes: 12 additions & 12 deletions communication/graphql.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
export class GraphQL {
private _variables = {};
private _query: string = 'query{}';
private _query: string = "query{}";

public constructor(
private readonly endpoint = '/graphql'
private readonly endpoint = "/graphql",
) {
}

public execute() {
return fetch(this.endpoint, {
method: 'POST',
method: "POST",
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
"Content-Type": "application/json",
"Accept": "application/json",
},
body: JSON.stringify({
query: this._query,
variables: this._variables
})
}).then(r => r.json());
variables: this._variables,
}),
}).then((r) => r.json());
}

/**
Expand All @@ -41,7 +41,7 @@ export class GraphQL {
* @return The instance of this class
*/
public addVariable(key: string, value: string): GraphQL {
this._variables[key] = value;
return this;
this._variables[key] = value;
return this;
}
}
Loading

0 comments on commit e006027

Please sign in to comment.