1.0.2 • Published 11 months ago

@aliksend/fn v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

fn

This module allows to make functions with typechecking

Example:

import { z } from 'zod'

const typedParseInt = Fn.build({
  request: z.string(),
  response: z.number().int()
}).init((req) => {
  return parseInt(req)
})

const parsed = await typedParseInt('123')
console.log(parsed)

// Output:
// 123

It also supports AsyncIterators

import { z } from 'zod'

const fn = Fn.build({
  request: z.string(),
  response: z.number().int(),
  responseType: 'asynciterable',
}).init((req) => {
  const values = req.split(' ')
  return {
    [Symbol.asyncIterator]: () => ({
      async next () {
        if (values.length === 0) {
          return { done: true, value: undefined as any } // https://github.com/microsoft/TypeScript/issues/38479
        }
        return { value: parseInt(values.shift()!) }
      },
    }),
  }
})

for await (const parsed of fn('123 465')) {
  console.log(parsed)
}

// Output:
// 123
// 456

And middlewares like lock (don't allow to call callback simultaneuosly), cache and expiration (allows to set timeout for callback)

1.0.2

11 months ago

1.0.1

1 year ago

1.0.0

1 year ago