0.2.9 • Published 9 months ago

@yangcurve/actions v0.2.9

Weekly downloads
-
License
MIT
Repository
github
Last release
9 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

9 months ago

0.2.7

9 months ago

0.2.6

9 months ago

0.2.9

9 months ago

0.2.8

9 months ago

0.2.3

9 months ago

0.2.2

9 months ago

0.2.5

9 months ago

0.2.4

9 months ago

0.2.0

9 months ago

0.1.52

9 months ago

0.1.53

9 months ago

0.1.10

9 months ago

0.1.11

9 months ago

0.1.12

9 months ago

0.1.13

9 months ago

0.1.14

9 months ago

0.1.15

9 months ago

0.1.50

9 months ago

0.1.51

9 months ago

0.1.49

9 months ago

0.1.41

9 months ago

0.1.42

9 months ago

0.1.43

9 months ago

0.1.44

9 months ago

0.1.45

9 months ago

0.1.46

9 months ago

0.1.47

9 months ago

0.1.48

9 months ago

0.1.40

9 months ago

0.1.0

9 months ago

0.1.2

9 months ago

0.1.1

9 months ago

0.1.38

9 months ago

0.1.8

9 months ago

0.1.39

9 months ago

0.1.7

9 months ago

0.1.9

9 months ago

0.1.4

9 months ago

0.1.3

9 months ago

0.1.6

9 months ago

0.1.5

9 months ago

0.1.30

9 months ago

0.1.31

9 months ago

0.1.32

9 months ago

0.1.33

9 months ago

0.1.34

9 months ago

0.1.35

9 months ago

0.1.36

9 months ago

0.1.37

9 months ago

0.1.27

9 months ago

0.1.28

9 months ago

0.1.29

9 months ago

0.1.20

9 months ago

0.1.21

9 months ago

0.1.22

9 months ago

0.1.23

9 months ago

0.1.24

9 months ago

0.1.25

9 months ago

0.1.26

9 months ago

0.0.14

9 months ago

0.1.16

9 months ago

0.1.17

9 months ago

0.1.18

9 months ago

0.1.19

9 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