Skip to content

Commit 31a8615

Browse files
committed
fix(connect): set legacyGet header to true on cache.get and data.get #531
This will allow consumers who would like to use the legacy format to forcibly continue doing so, even after the default in hyper is set to the new API
1 parent 4843e27 commit 31a8615

File tree

5 files changed

+33
-15
lines changed

5 files changed

+33
-15
lines changed

packages/connect/deno/mod.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,7 @@ export * from "./types.ts";
3333

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

36-
export function connect(
37-
CONNECTION_STRING: string,
38-
// deno-lint-ignore no-inferrable-types
39-
domain: string = "default",
40-
): Hyper {
36+
export function connect(CONNECTION_STRING: string, domain = "default"): Hyper {
4137
const config = new URL(CONNECTION_STRING);
4238

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

67-
//const log = (x: any) => (console.log(x), x);
68-
6963
return {
7064
data: {
7165
add: (body) =>

packages/connect/deno/services/cache.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
HyperRequestFunction,
55
Method,
66
} from "../types.ts";
7+
import { HYPER_LEGACY_GET_HEADER } from "../utils/hyper-request.ts";
78

89
const service = "cache" as const;
910

@@ -14,21 +15,29 @@ export const add =
1415
(key: string, value: unknown, ttl?: string) => (h: HyperRequestFunction) =>
1516
h({ service, method: Method.POST, body: { key, value, ttl } });
1617

17-
export const get = (key: string) => (h: HyperRequestFunction) =>
18-
h({ service, method: Method.GET, resource: key });
18+
export const get = (key: string) => (h: HyperRequestFunction) => {
19+
return h({
20+
service,
21+
method: Method.GET,
22+
headers: new Headers({
23+
[HYPER_LEGACY_GET_HEADER]: "true",
24+
}),
25+
resource: key,
26+
});
27+
};
1928

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

2332
export const set =
2433
(key: string, value: unknown, ttl?: string) => (h: HyperRequestFunction) =>
2534
h(
26-
[{ service, method: Method.PUT, resource: key, body: value }]
27-
.map(includeTTL(ttl))[0],
35+
[{ service, method: Method.PUT, resource: key, body: value }].map(
36+
includeTTL(ttl),
37+
)[0],
2838
);
2939

30-
// deno-lint-ignore no-inferrable-types
31-
export const query = (pattern: string = "*") => (h: HyperRequestFunction) =>
40+
export const query = (pattern = "*") => (h: HyperRequestFunction) =>
3241
h({
3342
service,
3443
method: Method.POST,

packages/connect/deno/services/data.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,22 @@ import {
77
Method,
88
QueryOptions,
99
} from "../types.ts";
10+
import { HYPER_LEGACY_GET_HEADER } from "../utils/hyper-request.ts";
1011

1112
const service = "data" as const;
1213

1314
export const add = (body: unknown) => (hyper: HyperRequestFunction) =>
1415
hyper({ service, method: Method.POST, body });
15-
export const get = (id: string) => (hyper: HyperRequestFunction) =>
16-
hyper({ service, method: Method.GET, resource: id });
16+
export const get = (id: string) => (hyper: HyperRequestFunction) => {
17+
return hyper({
18+
service,
19+
method: Method.GET,
20+
headers: new Headers({
21+
[HYPER_LEGACY_GET_HEADER]: "true",
22+
}),
23+
resource: id,
24+
});
25+
};
1726
export const list =
1827
(options: ListOptions = {}) => (hyper: HyperRequestFunction) =>
1928
hyper({ service, method: Method.GET, params: options });

packages/connect/deno/tests/cache.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
remove,
1111
set,
1212
} from "../services/cache.ts";
13+
import { HYPER_LEGACY_GET_HEADER } from "../utils/hyper-request.ts";
1314

1415
const test = Deno.test;
1516

@@ -37,8 +38,10 @@ test("cache.get", async () => {
3738
assertEquals(h.service, "cache");
3839
assertEquals(h.method, "GET");
3940
assertEquals(h.resource, "game-1");
41+
assertEquals(h.headers?.get(HYPER_LEGACY_GET_HEADER), "true");
4042
return Promise.resolve(new Request("http://localhost"));
4143
};
44+
4245
await get("game-1")(mockRequest);
4346
});
4447

packages/connect/deno/tests/data.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
remove,
1414
update,
1515
} from "../services/data.ts";
16+
import { HYPER_LEGACY_GET_HEADER } from "../utils/hyper-request.ts";
1617

1718
const test = Deno.test;
1819

@@ -38,8 +39,10 @@ test("data.get", async () => {
3839
assertEquals(h.service, "data");
3940
assertEquals(h.method, "GET");
4041
assertEquals(h.resource, "game-1");
42+
assertEquals(h.headers?.get(HYPER_LEGACY_GET_HEADER), "true");
4143
return Promise.resolve(new Request("http://localhost"));
4244
};
45+
4346
await get("game-1")(mockRequest);
4447
});
4548

0 commit comments

Comments
 (0)