Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/connect/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ clean:
@rm -rf dist to-node

test:
@deno lint && deno fmt && deno test -A --unstable deno/tests/*.ts
@deno lint && deno fmt && deno test -A --no-lock --unstable deno/tests/*.ts

test-integration: clean to-node
@cd ./node && yarn && yarn test

to-node:
@deno run --no-check --allow-read --allow-write \
https://deno.land/x/deno2node@v1.3.0/src/cli.ts ./tsconfig.to-node.jsonc && \
https://deno.land/x/deno2node@v1.6.0/src/cli.ts ./tsconfig.to-node.jsonc && \
yarn build && \
echo "copying types..." && find ./to-node -mindepth 1 -depth -name '*.d.ts' -exec cp --parents \{\} ./dist \; && \
mv ./dist/to-node/* ./dist && rm -rf ./dist/to-node
Expand Down
2 changes: 1 addition & 1 deletion packages/connect/deno/dev_deps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export {
assert,
assertEquals,
} from "https://deno.land/std@0.150.0/testing/asserts.ts";
} from "https://deno.land/std@0.167.0/testing/asserts.ts";
8 changes: 1 addition & 7 deletions packages/connect/deno/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ export * from "./types.ts";

export { createHyperVerify } from "./utils/hyper-verify.ts";

export function connect(
CONNECTION_STRING: string,
// deno-lint-ignore no-inferrable-types
domain: string = "default",
): Hyper {
export function connect(CONNECTION_STRING: string, domain = "default"): Hyper {
const config = new URL(CONNECTION_STRING);

const h = async (hyperRequest: HyperRequest) => {
Expand All @@ -64,8 +60,6 @@ export function connect(
.then((r) => (response.ok ? r : assoc("status", response.status, r)))
.then((r) => (response.status >= 500 ? Promise.reject(r) : r));

//const log = (x: any) => (console.log(x), x);

return {
data: {
add: (body) =>
Expand Down
21 changes: 15 additions & 6 deletions packages/connect/deno/services/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
HyperRequestFunction,
Method,
} from "../types.ts";
import { HYPER_LEGACY_GET_HEADER } from "../utils/hyper-request.ts";

const service = "cache" as const;

Expand All @@ -14,21 +15,29 @@ 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) => {
return h({
service,
method: Method.GET,
headers: new Headers({
[HYPER_LEGACY_GET_HEADER]: "true",
}),
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) =>
h(
[{ service, method: Method.PUT, resource: key, body: value }]
.map(includeTTL(ttl))[0],
[{ 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) =>
export const query = (pattern = "*") => (h: HyperRequestFunction) =>
h({
service,
method: Method.POST,
Expand Down
13 changes: 11 additions & 2 deletions packages/connect/deno/services/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,22 @@ import {
Method,
QueryOptions,
} from "../types.ts";
import { HYPER_LEGACY_GET_HEADER } from "../utils/hyper-request.ts";

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 get = (id: string) => (hyper: HyperRequestFunction) => {
return hyper({
service,
method: Method.GET,
headers: new Headers({
[HYPER_LEGACY_GET_HEADER]: "true",
}),
resource: id,
});
};
export const list =
(options: ListOptions = {}) => (hyper: HyperRequestFunction) =>
hyper({ service, method: Method.GET, params: options });
Expand Down
3 changes: 3 additions & 0 deletions packages/connect/deno/tests/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
remove,
set,
} from "../services/cache.ts";
import { HYPER_LEGACY_GET_HEADER } from "../utils/hyper-request.ts";

const test = Deno.test;

Expand Down Expand Up @@ -37,8 +38,10 @@ test("cache.get", async () => {
assertEquals(h.service, "cache");
assertEquals(h.method, "GET");
assertEquals(h.resource, "game-1");
assertEquals(h.headers?.get(HYPER_LEGACY_GET_HEADER), "true");
return Promise.resolve(new Request("http://localhost"));
};

await get("game-1")(mockRequest);
});

Expand Down
3 changes: 3 additions & 0 deletions packages/connect/deno/tests/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
remove,
update,
} from "../services/data.ts";
import { HYPER_LEGACY_GET_HEADER } from "../utils/hyper-request.ts";

const test = Deno.test;

Expand All @@ -38,8 +39,10 @@ test("data.get", async () => {
assertEquals(h.service, "data");
assertEquals(h.method, "GET");
assertEquals(h.resource, "game-1");
assertEquals(h.headers?.get(HYPER_LEGACY_GET_HEADER), "true");
return Promise.resolve(new Request("http://localhost"));
};

await get("game-1")(mockRequest);
});

Expand Down
144 changes: 135 additions & 9 deletions packages/connect/deno/tests/hyper-request.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,140 @@
import { assert, assertEquals } from "../dev_deps.ts";

import { generateToken } from "../deps.deno.ts";
import { hyper, HYPER_LEGACY_GET_HEADER } from "../utils/hyper-request.ts";
import { HyperRequest } from "../types.ts";

Deno.test("generateToken", async () => {
try {
const res = await generateToken("SUB", "SECRET");
assert(true);
assertEquals(typeof res, "string");
// deno-lint-ignore no-explicit-any
} catch (error: any) {
assert(false, error.message);
}
Deno.test("hyper-request", async (t) => {
await t.step("generateToken", async (t) => {
await t.step("should generate a token successfully", async () => {
try {
const res = await generateToken("SUB", "SECRET");
assert(true);
assertEquals(typeof res, "string");
// deno-lint-ignore no-explicit-any
} catch (error: any) {
assert(false, error.message);
}
});
});

await t.step("hyper", async (t) => {
const req: HyperRequest = {
service: "data",
method: "GET",
};

await t.step("url", async (t) => {
await t.step("it should append params", async () => {
const resource = await hyper(
new URL("cloud://mock.hyper.io/foobar"),
"default",
)({
...req,
params: {
foo: "bar",
},
});
assertEquals(
resource.url,
"https://mock.hyper.io/foobar/data/default?foo=bar",
);
});

await t.step("isCloud", async (t) => {
const cloudCs = "cloud://mock.hyper.io/foobar";

await t.step("should build the resource url correctly", async () => {
const resource = await hyper(new URL(cloudCs), "default")(req);
assertEquals(
resource.url,
"https://mock.hyper.io/foobar/data/default",
);
});

await t.step("should build the action url correctly", async () => {
const action = await hyper(
new URL(cloudCs),
"default",
)({ ...req, action: "_bulk" });
assertEquals(
action.url,
"https://mock.hyper.io/foobar/data/default/_bulk",
);
});
});

await t.step("not isCloud", async (t) => {
const notCloud = "http://localhost:6363/foobar";

await t.step("should build the resource url correctly", async () => {
const resource = await hyper(new URL(notCloud), "default")(req);
assertEquals(resource.url, "http://localhost:6363/data/foobar");
});

await t.step("should build the action url correctly", async () => {
const action = await hyper(
new URL(notCloud),
"default",
)({ ...req, action: "_bulk" });
assertEquals(action.url, "http://localhost:6363/data/foobar/_bulk");
});
});
});

await t.step("options", async (t) => {
await t.step("headers", async (t) => {
await t.step("should set the Authorization header", async () => {
const resource = await hyper(
new URL("http://foo:bar@localhost:6363/foobar"),
"default",
)(req);
assert(resource.options?.headers.has("Authorization"));
});

await t.step("should set the Content-Type header", async () => {
const resource = await hyper(
new URL("http://foo:bar@localhost:6363/foobar"),
"default",
)(req);
assert(resource.options?.headers.has("Content-Type"));
});

await t.step("should set any provided headers", async () => {
const resource = await hyper(
new URL("http://foo:bar@localhost:6363/foobar"),
"default",
)({
...req,
headers: new Headers({ [HYPER_LEGACY_GET_HEADER]: "true" }),
});
assert(resource.options?.headers.has(HYPER_LEGACY_GET_HEADER));
});
});

await t.step("should add the body", async () => {
const resource = await hyper(
new URL("http://localhost:6363/foobar"),
"default",
)({ ...req, body: { foo: "bar" } });
assertEquals(resource.options?.body, JSON.stringify({ foo: "bar" }));
});

await t.step("should add the method", async () => {
const resource = await hyper(
new URL("http://localhost:6363/foobar"),
"default",
)(req);
assertEquals(resource.options?.method, "GET");

const resource2 = await hyper(
new URL("http://localhost:6363/foobar"),
"default",
// deno-lint-ignore ban-ts-comment
// @ts-ignore
)({ ...req, method: undefined });
assertEquals(resource2.options?.method, "GET");
});
});
});
});
1 change: 1 addition & 0 deletions packages/connect/deno/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ export interface HyperRequest {
service: "data" | "cache" | "storage" | "search" | "queue" | "info";
method: Method;
resource?: string;
headers?: Headers;
body?: unknown;
// deno-lint-ignore no-explicit-any
params?: undefined | Record<string, any>;
Expand Down
15 changes: 12 additions & 3 deletions packages/connect/deno/utils/hyper-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,24 @@ interface HyperRequestParams {
options?: RequestOptions;
}

export const HYPER_LEGACY_GET_HEADER = "X-HYPER-LEGACY-GET";

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

let options = {
headers: new Headers({
...(headers ? Object.fromEntries(headers.entries()) : {}),
"Content-Type": "application/json",
}),
method: method ? method : Method.GET,
Expand Down
10 changes: 5 additions & 5 deletions packages/connect/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@
"build": "microbundle --tsconfig tsconfig.ci.jsonc"
},
"dependencies": {
"@deno/shim-deno": "^0.9.0",
"@deno/shim-deno": "^0.11.0",
"crocks": "^0.12.4",
"jose": "^4.9.1",
"jose": "^4.11.1",
"ms": "^2.1.3",
"ramda": "^0.28.0",
"undici": "^5.10.0"
"undici": "^5.14.0"
},
"devDependencies": {
"@types/ms": "^0.7.31",
"@types/node": "^16.11.56",
"@types/ramda": "^0.28.15",
"@types/node": "^18.11.14",
"@types/ramda": "^0.28.20",
"microbundle": "^0.15.1"
}
}
Loading