Skip to content

Instantly share code, notes, and snippets.

@Grubba27
Created September 15, 2022 18:57
Show Gist options
  • Select an option

  • Save Grubba27/a13043dc91a23060f2f8ae07785ee4c1 to your computer and use it in GitHub Desktop.

Select an option

Save Grubba27/a13043dc91a23060f2f8ae07785ee4c1 to your computer and use it in GitHub Desktop.
Draft of evoved metheor methods
import type {Application} from './server'
const client = () => {
const call = <Args>(name: string, args: Args) => {
// impl....
}
return{
call
} as Application
}
// ...
const b = await client().call('bar', {test: 0})
// ^? b is number
const a = await client().call('foo', {ops: 0}) // <- err
const c = await client().call('foo', {str: ''})
// ^? c is string
import {Meteor} from 'meteor/meteor';
type App =
<Name extends string, P, R>(
methodName: Name,
validator: () => P,
query: (arg: P) => Promise<R>
) => { call: (name: Name, args: P) => Promise<R> }
const createMethod: App =
( methodName, validator, query) => {
if (!validator()) {
throw new Error('Invalid arguments')
}
Meteor.methods({[methodName]: query})
const call = (name: typeof methodName, args: ReturnType<typeof validator> ) =>
new Promise((resolve, reject) =>
Meteor.call(name, args, reject, resolve)) as ReturnType<typeof query>
return {call}
}
const foo = createMethod(
'foo',
() => ({str: ''}),
({str}) => new Promise<string>(resolve => resolve(str))
)
const bar = createMethod(
'bar',
() => ({test: 0}),
({num}) => new Promise<number>(resolve => resolve(num))
)
const meteorTest = createMethod(
'any.name',
() => ({foo: 'bar', hehe: 0}),
({foo, hehe}) => {
return new Promise<{foo: 'bar', hehe: 0}>(resolve => resolve({foo, hehe}))
}
)
export type Application = typeof foo & typeof bar & typeof meteorTest; // :( <- maybe Declaration Merging?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment