1.0.0-rc.31 • Published 12 months ago

@exobase/use-services v1.0.0-rc.31

Weekly downloads
-
License
MIT
Repository
-
Last release
12 months ago

title: 'useServices' description: 'A hook inject needed services into an Exobase function'

group: 'Hooks'

Provides an Exobase hook that allows for pseudo dependency injection of endpoint function dependencies into the services property of the Props argument.

Install

yarn add @exobase/use-services
# or
yarn add @exobase/hooks

Import

import { useServices } from '@exobase/use-services'
// or
import { useServices } from '@exobase/hooks'

Usage

To keep your endpoint functions easy to test, use the useServices hook to pass modules that own the interface to an external resources (database, cache, or a third-party app) as a function argument.

import { compose } from 'radash'
import type { Props } from '@exobase/core'
import { useExpress } from '@exobase/use-express'
import { useServices } from '@exobase/use-services'

type Args = {}
type Services = {
  database: Database
  cache: Cache
  stripe: Stripe
  github: GitHub
}

const setupAccount = ({ services }: Props<Args, Services>) => {
  const { database, cache, stripe, github } = services
  const customer = await database.customers.get()
  const paymentData = await stripe.setup(customer)
  await github.cloneRepository(customer)
  await cache.put(customer)
}

export default compose(
  useExpress(),
  useServices({
    database: () => new Database(),
    cache: () => new Cache(),
    stripe: () => new Stripe(),
    github: () => new GitHub()
  }),
  setupAccount
)
1.0.0-rc.31

12 months ago

1.0.0-rc.30

1 year ago

1.0.0-rc.29

1 year ago

1.0.0-rc.28

1 year ago

1.0.0-rc.27

1 year ago

1.0.0-rc.26

1 year ago

1.0.0-rc.24

2 years ago

1.0.0-rc.23

2 years ago

1.0.0-rc.22

2 years ago

1.0.0-rc.21

2 years ago

1.0.0-rc.20

2 years ago

1.0.0-rc.19

2 years ago

1.0.0-rc.13

3 years ago

1.0.0-rc.17

2 years ago

1.0.0-rc.16

2 years ago

1.0.0-rc.15

2 years ago

1.0.0-rc.14

2 years ago

1.0.0-rc.18

2 years ago

1.0.0-rc.12

3 years ago

1.0.0-rc.11

3 years ago

1.0.0-rc.10

3 years ago

1.0.0-rc.9

3 years ago

1.0.0-rc.8

3 years ago

1.0.0-rc.7

3 years ago

1.0.0-rc.6

3 years ago

1.0.0-rc.5

3 years ago

1.0.0-rc.4

3 years ago

1.0.0-rc.3

3 years ago

1.0.0-rc.2

3 years ago