Skip to content

Commit

Permalink
style(connect): deno fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
TillaTheHun0 committed Aug 30, 2022
1 parent aacb2b0 commit c42a749
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 128 deletions.
54 changes: 25 additions & 29 deletions packages/connect/deno/services/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,39 @@ import {

const service = "cache" as const;

const includeTTL = (ttl: string | undefined) =>
(o: HyperRequest) => ttl ? { ...o, params: { ttl } } : o;
const includeTTL = (ttl: string | undefined) => (o: HyperRequest) =>
ttl ? { ...o, params: { ttl } } : o;

export const add = (key: string, value: unknown, ttl?: string) =>
(h: HyperRequestFunction) =>
export const add =
(key: string, value: unknown, ttl?: string) => (h: HyperRequestFunction) =>
h({ service, method: Method.POST, body: { key, value, ttl } });

export const get = (key: string) =>
(h: HyperRequestFunction) =>
h({ service, method: Method.GET, resource: key });
export const get = (key: string) => (h: HyperRequestFunction) =>
h({ service, method: Method.GET, resource: key });

export const remove = (key: string) =>
(h: HyperRequestFunction) =>
h({ service, method: Method.DELETE, resource: key });
export const remove = (key: string) => (h: HyperRequestFunction) =>
h({ service, method: Method.DELETE, resource: key });

export const set = (key: string, value: unknown, ttl?: string) =>
(h: HyperRequestFunction) =>
export const set =
(key: string, value: unknown, ttl?: string) => (h: HyperRequestFunction) =>
h(
[{ service, method: Method.PUT, resource: key, body: value }]
.map(includeTTL(ttl))[0],
);

// deno-lint-ignore no-inferrable-types
export const query = (pattern: string = "*") =>
(h: HyperRequestFunction) =>
h({
service,
method: Method.POST,
action: Action.QUERY,
params: { pattern },
});

export const create = () =>
(hyper: HyperRequestFunction) => hyper({ service, method: Method.PUT });

export const destroy = (confirm = true) =>
(hyper: HyperRequestFunction) =>
confirm
? hyper({ service, method: Method.DELETE })
: Promise.reject({ ok: false, msg: "request not confirmed!" });
export const query = (pattern: string = "*") => (h: HyperRequestFunction) =>
h({
service,
method: Method.POST,
action: Action.QUERY,
params: { pattern },
});

export const create = () => (hyper: HyperRequestFunction) =>
hyper({ service, method: Method.PUT });

export const destroy = (confirm = true) => (hyper: HyperRequestFunction) =>
confirm
? hyper({ service, method: Method.DELETE })
: Promise.reject({ ok: false, msg: "request not confirmed!" });
48 changes: 22 additions & 26 deletions packages/connect/deno/services/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,31 @@ import {

const service = "data" as const;

export const add = (body: unknown) =>
(hyper: HyperRequestFunction) =>
hyper({ service, method: Method.POST, body });
export const get = (id: string) =>
(hyper: HyperRequestFunction) =>
hyper({ service, method: Method.GET, resource: id });
export const list = (options: ListOptions = {}) =>
(hyper: HyperRequestFunction) =>
export const add = (body: unknown) => (hyper: HyperRequestFunction) =>
hyper({ service, method: Method.POST, body });
export const get = (id: string) => (hyper: HyperRequestFunction) =>
hyper({ service, method: Method.GET, resource: id });
export const list =
(options: ListOptions = {}) => (hyper: HyperRequestFunction) =>
hyper({ service, method: Method.GET, params: options });
export const update = (id: string, doc: unknown) =>
(hyper: HyperRequestFunction) =>
export const update =
(id: string, doc: unknown) => (hyper: HyperRequestFunction) =>
hyper({ service, method: Method.PUT, resource: id, body: doc });
export const remove = (id: string) =>
(hyper: HyperRequestFunction) =>
hyper({ service, method: Method.DELETE, resource: id });
export const query = (selector: unknown, options?: QueryOptions) =>
export const remove = (id: string) => (hyper: HyperRequestFunction) =>
hyper({ service, method: Method.DELETE, resource: id });
export const query =
(selector: unknown, options?: QueryOptions) =>
(hyper: HyperRequestFunction) =>
hyper({
service,
method: Method.POST,
action: Action.QUERY,
body: toDataQuery(selector, options),
});
export const bulk = (docs: unknown[]) =>
(hyper: HyperRequestFunction) =>
hyper({ service, method: Method.POST, action: Action.BULK, body: docs });
export const index = (indexName: string, fields: string[]) =>
(hyper: HyperRequestFunction) =>
export const bulk = (docs: unknown[]) => (hyper: HyperRequestFunction) =>
hyper({ service, method: Method.POST, action: Action.BULK, body: docs });
export const index =
(indexName: string, fields: string[]) => (hyper: HyperRequestFunction) =>
hyper({
service,
method: Method.POST,
Expand All @@ -49,11 +46,10 @@ export const index = (indexName: string, fields: string[]) =>
},
});

export const create = () =>
(hyper: HyperRequestFunction) => hyper({ service, method: Method.PUT });
export const create = () => (hyper: HyperRequestFunction) =>
hyper({ service, method: Method.PUT });

export const destroy = (confirm = true) =>
(hyper: HyperRequestFunction) =>
confirm
? hyper({ service, method: Method.DELETE })
: Promise.reject({ ok: false, msg: "request not confirmed!" });
export const destroy = (confirm = true) => (hyper: HyperRequestFunction) =>
confirm
? hyper({ service, method: Method.DELETE })
: Promise.reject({ ok: false, msg: "request not confirmed!" });
4 changes: 2 additions & 2 deletions packages/connect/deno/services/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import { HyperRequestFunction, Method } from "../types.ts";

const service = "info" as const;

export const services = () =>
(h: HyperRequestFunction) => h({ service, method: Method.GET });
export const services = () => (h: HyperRequestFunction) =>
h({ service, method: Method.GET });
14 changes: 6 additions & 8 deletions packages/connect/deno/services/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ import { HyperRequestFunction, Method, QueueStatus } from "../types.ts";

const service = "queue" as const;

export const enqueue = (body: unknown) =>
(h: HyperRequestFunction) => h({ service, method: Method.POST, body });
export const enqueue = (body: unknown) => (h: HyperRequestFunction) =>
h({ service, method: Method.POST, body });

export const errors = () =>
(h: HyperRequestFunction) =>
h({ service, method: Method.GET, params: { status: QueueStatus.ERROR } });
export const errors = () => (h: HyperRequestFunction) =>
h({ service, method: Method.GET, params: { status: QueueStatus.ERROR } });

export const queued = () =>
(h: HyperRequestFunction) =>
h({ service, method: Method.GET, params: { status: QueueStatus.READY } });
export const queued = () => (h: HyperRequestFunction) =>
h({ service, method: Method.GET, params: { status: QueueStatus.READY } });
39 changes: 18 additions & 21 deletions packages/connect/deno/services/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,22 @@ const { lensPath, set } = R;

const service = "search" as const;

export const add = (key: string, doc: unknown) =>
(hyper: HyperRequestFunction) =>
export const add =
(key: string, doc: unknown) => (hyper: HyperRequestFunction) =>
hyper({ service, method: Method.POST, body: { key, doc } });

export const remove = (key: string) =>
(hyper: HyperRequestFunction) =>
hyper({ service, method: Method.DELETE, resource: key });
export const remove = (key: string) => (hyper: HyperRequestFunction) =>
hyper({ service, method: Method.DELETE, resource: key });

export const get = (key: string) =>
(hyper: HyperRequestFunction) =>
hyper({ service, method: Method.GET, resource: key });
export const get = (key: string) => (hyper: HyperRequestFunction) =>
hyper({ service, method: Method.GET, resource: key });

export const update = (key: string, doc: unknown) =>
(hyper: HyperRequestFunction) =>
export const update =
(key: string, doc: unknown) => (hyper: HyperRequestFunction) =>
hyper({ service, method: Method.PUT, resource: key, body: doc });

export const query = (query: string, options?: SearchQueryOptions) =>
export const query =
(query: string, options?: SearchQueryOptions) =>
(hyper: HyperRequestFunction) =>
hyper(
[{ service, method: Method.POST, action: Action.QUERY, body: { query } }]
Expand All @@ -43,16 +42,14 @@ export const query = (query: string, options?: SearchQueryOptions) =>
)[0],
);

export const load = (docs: unknown[]) =>
(hyper: HyperRequestFunction) =>
hyper({ service, method: Method.POST, action: Action.BULK, body: docs });
export const load = (docs: unknown[]) => (hyper: HyperRequestFunction) =>
hyper({ service, method: Method.POST, action: Action.BULK, body: docs });

export const create = (fields: string[], storeFields?: string[]) =>
(hyper: HyperRequestFunction) =>
export const create =
(fields: string[], storeFields?: string[]) => (hyper: HyperRequestFunction) =>
hyper({ service, method: Method.PUT, body: { fields, storeFields } });

export const destroy = (confirm = true) =>
(hyper: HyperRequestFunction) =>
confirm
? hyper({ service, method: Method.DELETE })
: Promise.reject({ ok: false, msg: "request not confirmed!" });
export const destroy = (confirm = true) => (hyper: HyperRequestFunction) =>
confirm
? hyper({ service, method: Method.DELETE })
: Promise.reject({ ok: false, msg: "request not confirmed!" });
84 changes: 42 additions & 42 deletions packages/connect/deno/utils/hyper-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,45 @@ interface HyperRequestParams {
}

export const hyper = (conn: URL, domain: string) =>
async (
{ service, method, resource, body, params, action }: HyperRequest,
): Promise<HyperRequestParams> => {
const isCloud = /^cloud/.test(conn.protocol);
const protocol = isCloud ? "https:" : conn.protocol;

let options = {
headers: new Headers({
"Content-Type": "application/json",
}),
method: method ? method : Method.GET,
} as RequestOptions;

if (body) {
options = assoc("body", JSON.stringify(body), options) as RequestOptions;
}

if (conn.username && conn.password) {
const token = await generateToken(conn.username, conn.password);
options.headers = new Headers({
...Object.fromEntries(options.headers.entries()),
Authorization: `Bearer ${token}`,
});
}
const pathname = isCloud ? conn.pathname : "";
const appdomain = isCloud ? "/" + domain : conn.pathname;

let url = `${protocol}//${conn.host}${pathname}/${service}${appdomain}`;

if (service === "info") {
url = `${protocol}//${conn.host}`;
}

if (resource) url += `/${resource}`;
else if (action) url += `/${action}`;

if (params) {
url += `?${new URLSearchParams(params).toString()}`;
}

return { url, options };
};
async (
{ service, method, resource, body, params, action }: HyperRequest,
): Promise<HyperRequestParams> => {
const isCloud = /^cloud/.test(conn.protocol);
const protocol = isCloud ? "https:" : conn.protocol;

let options = {
headers: new Headers({
"Content-Type": "application/json",
}),
method: method ? method : Method.GET,
} as RequestOptions;

if (body) {
options = assoc("body", JSON.stringify(body), options) as RequestOptions;
}

if (conn.username && conn.password) {
const token = await generateToken(conn.username, conn.password);
options.headers = new Headers({
...Object.fromEntries(options.headers.entries()),
Authorization: `Bearer ${token}`,
});
}
const pathname = isCloud ? conn.pathname : "";
const appdomain = isCloud ? "/" + domain : conn.pathname;

let url = `${protocol}//${conn.host}${pathname}/${service}${appdomain}`;

if (service === "info") {
url = `${protocol}//${conn.host}`;
}

if (resource) url += `/${resource}`;
else if (action) url += `/${action}`;

if (params) {
url += `?${new URLSearchParams(params).toString()}`;
}

return { url, options };
};

0 comments on commit c42a749

Please sign in to comment.