mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-05-11 02:10:02 +02:00
Add domain filter to cookie template function
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
"description": "Template functions for working with cookies",
|
||||
"scripts": {
|
||||
"build": "yaakcli build",
|
||||
"dev": "yaakcli dev"
|
||||
"dev": "yaakcli dev",
|
||||
"test": "vp test --run tests"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,11 +12,22 @@ export const plugin: PluginDefinition = {
|
||||
name: "name",
|
||||
label: "Cookie Name",
|
||||
},
|
||||
{
|
||||
type: "text",
|
||||
name: "domain",
|
||||
label: "Domain",
|
||||
optional: true,
|
||||
},
|
||||
],
|
||||
async onRender(ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> {
|
||||
// The legacy name was cookie_name, but we changed it
|
||||
const name = args.values.cookie_name ?? args.values.name;
|
||||
return ctx.cookies.getValue({ name: String(name) });
|
||||
const domain = String(args.values.domain ?? "").trim();
|
||||
|
||||
return ctx.cookies.getValue({
|
||||
name: String(name),
|
||||
...(domain.length > 0 ? { domain } : {}),
|
||||
});
|
||||
},
|
||||
},
|
||||
],
|
||||
|
||||
62
plugins/template-function-cookie/tests/index.test.ts
Normal file
62
plugins/template-function-cookie/tests/index.test.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import type { Context } from "@yaakapp/api";
|
||||
import { describe, expect, it, vi } from "vite-plus/test";
|
||||
import { plugin } from "../src";
|
||||
|
||||
describe("cookie.value", () => {
|
||||
const valueFunction = plugin.templateFunctions?.find((f) => f.name === "cookie.value");
|
||||
|
||||
it("should exist", () => {
|
||||
expect(valueFunction).toBeDefined();
|
||||
});
|
||||
|
||||
it("should get a cookie by name", async () => {
|
||||
const getValue = vi.fn().mockResolvedValue("token");
|
||||
|
||||
const result = await valueFunction?.onRender(
|
||||
{ cookies: { getValue } } as unknown as Context,
|
||||
{
|
||||
values: {
|
||||
name: "co-auth",
|
||||
},
|
||||
purpose: "send",
|
||||
},
|
||||
);
|
||||
|
||||
expect(result).toBe("token");
|
||||
expect(getValue).toHaveBeenCalledWith({ name: "co-auth" });
|
||||
});
|
||||
|
||||
it("should get a cookie by name and domain", async () => {
|
||||
const getValue = vi.fn().mockResolvedValue("token");
|
||||
|
||||
const result = await valueFunction?.onRender(
|
||||
{ cookies: { getValue } } as unknown as Context,
|
||||
{
|
||||
values: {
|
||||
name: "co-auth",
|
||||
domain: " example.com ",
|
||||
},
|
||||
purpose: "send",
|
||||
},
|
||||
);
|
||||
|
||||
expect(result).toBe("token");
|
||||
expect(getValue).toHaveBeenCalledWith({ name: "co-auth", domain: "example.com" });
|
||||
});
|
||||
|
||||
it("should support the legacy cookie_name arg", async () => {
|
||||
const getValue = vi.fn().mockResolvedValue("token");
|
||||
|
||||
await valueFunction?.onRender(
|
||||
{ cookies: { getValue } } as unknown as Context,
|
||||
{
|
||||
values: {
|
||||
cookie_name: "co-auth",
|
||||
},
|
||||
purpose: "send",
|
||||
},
|
||||
);
|
||||
|
||||
expect(getValue).toHaveBeenCalledWith({ name: "co-auth" });
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user