Skip to content

Commit

Permalink
feat: fetch_context utils
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-0acf4 committed Nov 28, 2024
1 parent 822437b commit 91e9e18
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/typegraph/deno/src/runtimes/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@ export class DenoRuntime extends Runtime {
return t.func(t.struct({}), out, mat);
}

/** Utility for fetching the current request context */
fetchContext<C extends t.Typedef>(outputShape?: C): t.Func {
const returnValue = outputShape ? `context` : "JSON.stringify(context)";
return this.func(
t.struct({}),
outputShape ?? t.json(),
{ code: `(_, { context }) => ${returnValue}` }
);
}

policy(name: string, _code: string): Policy;
policy(name: string, data: Omit<DenoFunc, "effect">): Policy;
policy(name: string, data: string | Omit<DenoFunc, "effect">): Policy {
Expand Down
17 changes: 16 additions & 1 deletion src/typegraph/python/typegraph/runtimes/deno.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import json
import re
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any, List, Optional
from typing import TYPE_CHECKING, Any, List, Optional, Union

from typegraph.gen.exports.runtimes import (
Effect,
Expand Down Expand Up @@ -129,6 +129,21 @@ def identity(self, inp: "t.struct") -> "t.func":
PredefinedFunMat(id=res.value, name="identity", effect=EffectRead()),
)

def fetch_context(self, output_shape: Union["t.struct", None] = None):
"""
Utility for fetching the current request context
"""
return_value = (
"context" if output_shape is not None else "JSON.stringify(context)"
)
from typegraph import t

return self.func(
inp=t.struct(),
out=output_shape or t.json(),
code=f"(_, {{ context }}) => {return_value}",
)

def policy(
self, name: str, code: str, secrets: Optional[List[str]] = None
) -> Policy:
Expand Down
17 changes: 17 additions & 0 deletions tests/auth/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,21 @@ def auth(g: Graph):
),
auth_token_field="token",
).with_policy(public),
# context_raw=deno.fetch_context().with_policy(public) # no args if shape is unknown
context=deno.fetch_context(
t.struct(
{
"provider": t.string(),
"accessToken": t.string(),
"refreshAt": t.integer(),
"profile": t.struct(
{
"id": t.integer(),
}
),
"exp": t.integer(),
"iat": t.integer(),
}
)
).with_policy(public),
)
12 changes: 12 additions & 0 deletions tests/auth/auth_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,12 @@ Meta.test("Auth", async (t) => {
token(x: 1) {
x
}
context {
provider
profile {
id
}
}
}
`
.withHeaders({ authorization: `bearer ${jwt}` })
Expand All @@ -347,6 +353,12 @@ Meta.test("Auth", async (t) => {
token: {
x: 1,
},
context: {
provider: "github",
profile: {
id: 123
}
}
})
.on(e);
});
Expand Down

0 comments on commit 91e9e18

Please sign in to comment.