Skip to content

Commit

Permalink
feat(connect)!: set legacyGet header to false on cache.get and data.get
Browse files Browse the repository at this point in the history
  • Loading branch information
TillaTheHun0 committed Dec 14, 2022
1 parent ad3b64c commit b9c5bc0
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 32 deletions.
8 changes: 7 additions & 1 deletion packages/connect/deno/services/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ export const get = (key: string) => (h: HyperRequestFunction) => {
service,
method: Method.GET,
headers: new Headers({
[HYPER_LEGACY_GET_HEADER]: "true",
/**
* Once the legacy flag is removed hyper,
* this will effectively do nothing
*
* TODO: remove when legacy flag is removed from hyper
*/
[HYPER_LEGACY_GET_HEADER]: "false",
}),
resource: key,
});
Expand Down
8 changes: 7 additions & 1 deletion packages/connect/deno/services/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ export const get = (id: string) => (hyper: HyperRequestFunction) => {
service,
method: Method.GET,
headers: new Headers({
[HYPER_LEGACY_GET_HEADER]: "true",
/**
* Once the legacy flag is removed hyper,
* this will effectively do nothing
*
* TODO: remove when legacy flag is removed from hyper
*/
[HYPER_LEGACY_GET_HEADER]: "false",
}),
resource: id,
});
Expand Down
2 changes: 1 addition & 1 deletion packages/connect/deno/tests/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ 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");
assertEquals(h.headers?.get(HYPER_LEGACY_GET_HEADER), "false");
return Promise.resolve(new Request("http://localhost"));
};

Expand Down
22 changes: 0 additions & 22 deletions packages/connect/deno/tests/connect.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/connect/deno/tests/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ 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");
assertEquals(h.headers?.get(HYPER_LEGACY_GET_HEADER), "false");
return Promise.resolve(new Request("http://localhost"));
};

Expand Down
39 changes: 39 additions & 0 deletions packages/connect/deno/tests/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// deno-lint-ignore-file ban-ts-comment
import { assertEquals } from "../dev_deps.ts";
import { connect } from "../mod.ts";

const test = Deno.test;

test("mod", async (t) => {
await t.step("not found", async () => {
globalThis.fetch = () =>
Promise.resolve(
new Response(JSON.stringify({ ok: false }), {
status: 404,
headers: { "content-type": "application/json" },
})
);

const hyper = connect("http://localhost:6363/test");
const result = await hyper.data.get("test-1");
assertEquals(result.ok, false);
// @ts-ignore
assertEquals(result.status, 404);
});

await t.step("found", async () => {
globalThis.fetch = () =>
Promise.resolve(
new Response(JSON.stringify({ ok: true, doc: { _id: "test-1" } }), {
status: 404,
headers: { "content-type": "application/json" },
})
);

const hyper = connect("http://localhost:6363/test");
const result = await hyper.data.get("test-1");
assertEquals(result.ok, true);
// @ts-ignore
assertEquals(result.doc, { _id: "test-1" });
});
});
16 changes: 10 additions & 6 deletions packages/connect/deno/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,16 @@ export interface QueryOptions {
useIndex?: string;
}

// TODO: exception to the rule of returning a Result shape.
// TODO: This will change to be just a regular result in a major version
export type HyperGetResult<Type extends Obj = Obj> = Type | NotOkResult;
export type HyperGetDocResult<Type extends Obj = Obj> =
| (OkResult & { doc: Type })
| NotOkResult;

export type HyperDocsResult<Type extends Obj = Obj> =
| (OkResult & { docs: Type[] })
| NotOkResult;
export interface HyperData {
add: <Type extends Obj = Obj>(doc: Type) => Promise<IdResult>;
get: <Type extends Obj = Obj>(id: string) => Promise<HyperGetResult<Type>>;
get: <Type extends Obj = Obj>(id: string) => Promise<HyperGetDocResult<Type>>;
list: <Type extends Obj = Obj>(
options?: ListOptions,
) => Promise<HyperDocsResult<Type>>;
Expand All @@ -100,7 +100,9 @@ export interface HyperCache {
value: Type,
ttl?: string,
) => Promise<Result>;
get: <Type extends Obj = Obj>(key: string) => Promise<HyperGetResult<Type>>;
get: <Type extends Obj = Obj>(
key: string,
) => Promise<HyperGetDocResult<Type>>;
remove: (key: string) => Promise<Result>;
set: <Type extends Obj = Obj>(
key: string,
Expand All @@ -123,7 +125,9 @@ export type HyperSearchLoadResult<Type extends Obj = Obj> =
export interface HyperSearch {
add: <Type extends Obj = Obj>(key: string, doc: Type) => Promise<Result>;
remove: (key: string) => Promise<Result>;
get: <Type extends Obj = Obj>(key: string) => Promise<HyperGetResult<Type>>;
get: <Type extends Obj = Obj>(
key: string,
) => Promise<(OkResult & { key: string; doc: Type }) | NotOkResult>;
update: <Type extends Obj = Obj>(key: string, doc: Type) => Promise<Result>;
query: <Type extends Obj = Obj>(
query: string,
Expand Down

0 comments on commit b9c5bc0

Please sign in to comment.