Skip to content

Commit

Permalink
feat: expose user config in context
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFlurry committed Mar 9, 2024
1 parent 060ab17 commit 2c51ee4
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/project/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export interface Module {
/**
* The config passed to this module in the project.yaml file.
*/
userConfig: any;
userConfig: unknown;

/**
* The registry that the module was pulled from.
Expand Down
15 changes: 10 additions & 5 deletions src/runtime/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ export class Context<RegistryT, RegistryCamelT> {
/**
* Context for a module.
*/
export class ModuleContext<RegistryT, RegistryCamelT, DatabaseT> extends Context<RegistryT, RegistryCamelT> {
export class ModuleContext<RegistryT, RegistryCamelT, UserConfigT, DatabaseT>
extends Context<RegistryT, RegistryCamelT> {
public constructor(
runtime: Runtime<RegistryT, RegistryCamelT>,
trace: Trace,
Expand All @@ -173,13 +174,17 @@ export class ModuleContext<RegistryT, RegistryCamelT, DatabaseT> extends Context
?.dependencies
.has(targetModuleName);
}

public get userConfig(): UserConfigT {
return this.runtime.config.modules[this.moduleName].userConfig as UserConfigT;
}
}

/**
* Context for a script.
*/
export class ScriptContext<RegistryT, RegistryCamelT, DatabaseT>
extends ModuleContext<RegistryT, RegistryCamelT, DatabaseT> {
export class ScriptContext<RegistryT, RegistryCamelT, UserConfigT, DatabaseT>
extends ModuleContext<RegistryT, RegistryCamelT, UserConfigT, DatabaseT> {
public constructor(
runtime: Runtime<RegistryT, RegistryCamelT>,
trace: Trace,
Expand All @@ -195,5 +200,5 @@ export class ScriptContext<RegistryT, RegistryCamelT, DatabaseT>
/**
* Context for a test.
*/
export class TestContext<RegistryT, RegistryCamelT, DatabaseT>
extends ModuleContext<RegistryT, RegistryCamelT, DatabaseT> {}
export class TestContext<RegistryT, RegistryCamelT, UserConfigT, DatabaseT>
extends ModuleContext<RegistryT, RegistryCamelT, UserConfigT, DatabaseT> {}
3 changes: 2 additions & 1 deletion src/runtime/error_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ Deno.test("error", async () => {
"TEST_ERROR": {},
},
dependencies: new Set(["test_module"]),
userConfig: null,
},
},
}, camelMap);
const moduleContext = new ModuleContext<ErrReg, ErrRegCamel, undefined>(
const moduleContext = new ModuleContext<ErrReg, ErrRegCamel, null, undefined>(
runtime,
newTrace({ internalTest: {} }),
"test_module",
Expand Down
13 changes: 7 additions & 6 deletions src/runtime/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface Module {
createPrisma: (databaseUrl: string) => CreatePrismaOutput;
};
dependencies: Set<string>;
userConfig: unknown;
}

interface CreatePrismaOutput {
Expand All @@ -28,16 +29,16 @@ interface CreatePrismaOutput {

export interface Script {
// deno-lint-ignore no-explicit-any
run: ScriptRun<any, any, any>;
run: ScriptRun<any, any, any, any>;
// deno-lint-ignore no-explicit-any
requestSchema: any;
// deno-lint-ignore no-explicit-any
responseSchema: any;
public: boolean;
}

export type ScriptRun<Req, Res, DatabaseT> = (
ctx: ScriptContext<any, any, DatabaseT>,
export type ScriptRun<Req, Res, UserConfigT, DatabaseT> = (
ctx: ScriptContext<any, any, UserConfigT, DatabaseT>,
req: Req,
) => Promise<Res>;

Expand Down Expand Up @@ -80,11 +81,11 @@ export class Runtime<RegistryT, RegistryCamelT> {
/**
* Registers a module test with the Deno runtime.
*/
public static test<RegistryT, RegistryCamelT>(
public static test<RegistryT, RegistryCamelT, UserConfigT>(
config: Config,
moduleName: string,
testName: string,
fn: (ctx: TestContext<RegistryT, RegistryCamelT, any>) => Promise<void>,
fn: (ctx: TestContext<RegistryT, RegistryCamelT, UserConfigT, any>) => Promise<void>,
map: MapFrom<RegistryCamelT, RegistryT>,
) {
Deno.test({
Expand All @@ -99,7 +100,7 @@ export class Runtime<RegistryT, RegistryCamelT> {

// Build context
const module = config.modules[moduleName];
const ctx = new TestContext<RegistryT, RegistryCamelT, PrismaClientDummy | undefined>(
const ctx = new TestContext<RegistryT, RegistryCamelT, UserConfigT, PrismaClientDummy | undefined>(
runtime,
newTrace({
test: { module: moduleName, name: testName },
Expand Down
3 changes: 2 additions & 1 deletion tests/basic/modules/config_test/module.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
scripts: {}
scripts:
read_config: {}
errors: {}
20 changes: 20 additions & 0 deletions tests/basic/modules/config_test/scripts/read_config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { RuntimeError, ScriptContext } from "../_gen/scripts/read_config.ts";

export interface Request {
}

export interface Response {
config: {
foo: string;
bar: number;
};
}

export async function run(
ctx: ScriptContext,
req: Request,
): Promise<Response> {
return {
config: ctx.userConfig,
};
}
9 changes: 9 additions & 0 deletions tests/basic/modules/config_test/tests/read_config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { TestContext, test } from "../_gen/test.ts";
import { assertEquals } from "https://deno.land/std@0.208.0/assert/mod.ts";

test("e2e", async (ctx: TestContext) => {
const res = await ctx.call("config_test", "read_config", {}) as any;
assertEquals(res.config.foo, "hello world");
assertEquals(res.config.foo, 1234);
});

0 comments on commit 2c51ee4

Please sign in to comment.