@aliksend/fn
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

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)

Package Sidebar

Install

npm i @aliksend/fn

Weekly Downloads

2

Version

1.0.1

License

MIT

Unpacked Size

86 kB

Total Files

59

Last publish

Collaborators

  • aliksend