0.2.9 • Published 10 months ago

@yangcurve/actions v0.2.9

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

Actions

Server Action wrapper inspired by trpc.

Installation

bun add @yangcurve/actions @tanstack/react-query zod

Usage

Initialize Actions

// init.ts
import { auth } from '@/server/auth'
import { db } from '@/server/db'
import { initActions } from '@yangcurve/actions'
import SuperJSON from 'superjson'

export const { createProcedure, createCaller, createClientCaller } = initActions({
  transformer: SuperJSON, // optional
})

Create procedures

// procedures.ts
import { createProcedure } from './init'
import { auth } from '@/server/auth'
import { db } from '@/server/db'

const createContext = async () => ({
  db,
  session: await auth(),
})

export const procedure = createProcedure({ createContext })
export const authorizedProcedure = procedure.use(({ ctx, next }) => {
  if (!ctx.session?.user) throw new Error('UNAUTHORIZED')
  return next({
    ...ctx,
    session: ctx.session,
  })
})

Create server actions

// count.ts
'use server'

import { procedure } from './procedures'
import { z } from 'zod'

let count = 0

export const get = procedure.query(() => count)
export const set = procedure.input(z.number()).mutation(({ input }) => (state = input))

Create callers

// caller.ts
import * as count from './count'
import { createCaller, createClientCaller } from './init'
import type { InferActionInput, InferActionOutput } from '@yangcurve/actions'

export const actions = createCaller({ count }) // server side caller
export const api = createClientCaller(actions) // client side caller

export type ActionInput = InferActionInput<typeof actions>
export type ActionOutput = InferActionOutput<typeof actions>

In server component

import { actions } from './caller'

export const ServerComponent = async () => {
  const count = await actions.count.get()
  ...
}

In client component

'use client'

import { api } from './caller'

export const ClientComponent = () => {
  const utils = api.useUtils()
  const { data: count } = api.count.get.useQuery()
  const { mutate: set } = api.count.set.useMutation({ onSuccess: () => utils.count.invalidate() })
  ...
}
0.2.1

10 months ago

0.2.7

10 months ago

0.2.6

10 months ago

0.2.9

10 months ago

0.2.8

10 months ago

0.2.3

10 months ago

0.2.2

10 months ago

0.2.5

10 months ago

0.2.4

10 months ago

0.2.0

10 months ago

0.1.52

10 months ago

0.1.53

10 months ago

0.1.10

10 months ago

0.1.11

10 months ago

0.1.12

10 months ago

0.1.13

10 months ago

0.1.14

10 months ago

0.1.15

10 months ago

0.1.50

10 months ago

0.1.51

10 months ago

0.1.49

10 months ago

0.1.41

10 months ago

0.1.42

10 months ago

0.1.43

10 months ago

0.1.44

10 months ago

0.1.45

10 months ago

0.1.46

10 months ago

0.1.47

10 months ago

0.1.48

10 months ago

0.1.40

10 months ago

0.1.0

10 months ago

0.1.2

10 months ago

0.1.1

10 months ago

0.1.38

10 months ago

0.1.8

10 months ago

0.1.39

10 months ago

0.1.7

10 months ago

0.1.9

10 months ago

0.1.4

10 months ago

0.1.3

10 months ago

0.1.6

10 months ago

0.1.5

10 months ago

0.1.30

10 months ago

0.1.31

10 months ago

0.1.32

10 months ago

0.1.33

10 months ago

0.1.34

10 months ago

0.1.35

10 months ago

0.1.36

10 months ago

0.1.37

10 months ago

0.1.27

10 months ago

0.1.28

10 months ago

0.1.29

10 months ago

0.1.20

10 months ago

0.1.21

10 months ago

0.1.22

10 months ago

0.1.23

10 months ago

0.1.24

10 months ago

0.1.25

10 months ago

0.1.26

10 months ago

0.0.14

10 months ago

0.1.16

10 months ago

0.1.17

10 months ago

0.1.18

10 months ago

0.1.19

10 months ago

0.0.11

1 year ago

0.0.12

1 year ago

0.0.13

1 year ago

0.0.10

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago