Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: return function when registering #22

Merged
merged 1 commit into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions javascript/src/rpc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import fs from 'fs-extra'
import nock from 'nock'
import crypto from 'crypto'
import { dedent } from 'ts-dedent'
import { describe, expect, beforeEach, afterEach, test, vi } from 'vitest'
import { describe, expect, beforeEach, afterEach, test, vi, expectTypeOf } from 'vitest'
import { v4 as uuidv4 } from 'uuid'

import { RetoolRPC } from './rpc'
import { Arguments } from './types'
import { Arguments, RetoolContext, TransformedArguments } from './types'
import { parseFunctionArguments } from './utils/schema'
import { RetoolRPCVersion } from './version'

Expand Down Expand Up @@ -751,6 +751,25 @@ describe('RetoolRPC', () => {
)
})
})

test('returns the implementation when registering', async () => {
const fn = rpcAgent.register({
name: 'test',
arguments: {},
implementation: async () => {
return 1
}
})

type ExpectedImplementation = (args: TransformedArguments<Arguments>, context: RetoolContext) => Promise<number>
expectTypeOf(fn).toEqualTypeOf<ExpectedImplementation>()


const result = await fn({}, context);
expect(result).toEqual(1)
expectTypeOf(result).toEqualTypeOf(1)
})

})

describe('RetoolRPCVersion', () => {
Expand Down
7 changes: 4 additions & 3 deletions javascript/src/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class RetoolRPC {
private _version: string
private _agentUuid: string
private _versionHash: string | undefined
private _functions: Record<string, Omit<RegisterFunctionSpec<any>, 'name'>> = {}
private _functions: Record<string, Omit<RegisterFunctionSpec<any, any>, 'name'>> = {}
private _retoolApi: RetoolAPI
private _logger: LoggerService

Expand Down Expand Up @@ -89,12 +89,13 @@ export class RetoolRPC {
/**
* Registers a Retool function with the specified function definition.
*/
register<TArgs extends Arguments>(spec: RegisterFunctionSpec<TArgs>): void {
register<TArgs extends Arguments, TReturn>(spec: RegisterFunctionSpec<TArgs, TReturn>): RegisterFunctionSpec<TArgs, TReturn>['implementation'] {
this._functions[spec.name] = {
arguments: spec.arguments,
permissions: spec.permissions,
implementation: spec.implementation,
}
return spec.implementation;
}

/**
Expand Down Expand Up @@ -145,7 +146,7 @@ export class RetoolRPC {
* Registers the agent with the Retool server.
*/
private async registerAgent(): Promise<AgentServerStatus> {
const functionsMetadata: Record<string, Pick<RegisterFunctionSpec<any>, 'arguments' | 'permissions'>> = {}
const functionsMetadata: Record<string, Pick<RegisterFunctionSpec<any, any>, 'arguments' | 'permissions'>> = {}
for (const functionName in this._functions) {
functionsMetadata[functionName] = {
arguments: this._functions[functionName].arguments,
Expand Down
4 changes: 2 additions & 2 deletions javascript/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ export type TransformedArguments<TArgs extends Arguments> = {
}

/** Represents the specification for registering a Retool function. */
export type RegisterFunctionSpec<TArgs extends Arguments> = {
export type RegisterFunctionSpec<TArgs extends Arguments, TReturn> = {
/** The name of the function. */
name: string
/** The arguments of the function. */
arguments: Pick<TArgs, keyof TArgs>
/** The implementation of the function. */
implementation: (args: TransformedArguments<TArgs>, context: RetoolContext) => Promise<any>
implementation: (args: TransformedArguments<TArgs>, context: RetoolContext) => Promise<TReturn>
/** The permissions configuration for the function. */
permissions?: {
/** The list of group names that have permission to execute the function. */
Expand Down
Loading