tRPCttRPC
Powered by
MugetsuM
tRPC•3y ago•
18 replies
Mugetsu

Meta - unable to create default meta

Trying to follow https://trpc.io/docs/server/metadata#default-meta-chaining-and-shallow-merging
I set the type of the meta add it to the builder and want to set default but seems there is no typing for the meta in the create to set the default. Am I missing something ?
"@trpc/client": "10.16.0",
    "@trpc/react-query": "10.16.0",
    "@trpc/server": "10.16.0",
"@trpc/client": "10.16.0",
    "@trpc/react-query": "10.16.0",
    "@trpc/server": "10.16.0",

type CreateContextOptions = {
  req: express.Request
  res: express.Response
  currentSchema: any
}

type MetaOptions = {
  roles: string[]
}

export const createInnerTRPCContext = async (opts: CreateContextOptions) => ({
  req: opts.req,
  res: opts.res,
  currentSchema: opts.currentSchema,
})

export const createTRPCContext = async (
  opts: trpcExpress.CreateExpressContextOptions,
) => {
  const { req, res } = opts
  const currentSchema =
    req.locationSchemas?.find(({ id }) => id === req.schemaId) || {}

  return createInnerTRPCContext({
    req,
    res,
    currentSchema,
  })
}

const t = initTRPC
  .context<typeof createTRPCContext>()
  .meta<MetaOptions>()
  .create({
/// defaultMeta <------------------------- MISSING TYPING
    transformer: superjson,
    errorFormatter({ shape, ctx }) {
      const { data, ...rest } = shape

      return {
        ...rest,
        data: {
          ...data,
          traceId: ctx?.req?.traceId,
          schemaId: ctx?.req?.schemaId,
        },
      }
    },
  })
type CreateContextOptions = {
  req: express.Request
  res: express.Response
  currentSchema: any
}

type MetaOptions = {
  roles: string[]
}

export const createInnerTRPCContext = async (opts: CreateContextOptions) => ({
  req: opts.req,
  res: opts.res,
  currentSchema: opts.currentSchema,
})

export const createTRPCContext = async (
  opts: trpcExpress.CreateExpressContextOptions,
) => {
  const { req, res } = opts
  const currentSchema =
    req.locationSchemas?.find(({ id }) => id === req.schemaId) || {}

  return createInnerTRPCContext({
    req,
    res,
    currentSchema,
  })
}

const t = initTRPC
  .context<typeof createTRPCContext>()
  .meta<MetaOptions>()
  .create({
/// defaultMeta <------------------------- MISSING TYPING
    transformer: superjson,
    errorFormatter({ shape, ctx }) {
      const { data, ...rest } = shape

      return {
        ...rest,
        data: {
          ...data,
          traceId: ctx?.req?.traceId,
          schemaId: ctx?.req?.schemaId,
        },
      }
    },
  })
Screenshot_2023-03-20_at_17.07.20.png
tRPCJoin
Move Fast & Break Nothing. End-to-end typesafe APIs made easy.
5,015Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

type safety with meta
BeanBBean / ❓-help
2y ago
Multiple .meta in one route
DevPandaDDevPanda / ❓-help
9mo ago
Unable to abort Suspense queries
phibr0Pphibr0 / ❓-help
2y ago
Extending "Meta" with downstream concat usage?
hamishHhamish / ❓-help
16mo ago