Skip to content

Commit

Permalink
Initializing readers and writers outside of the API request handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
mzkrasner committed Apr 24, 2024
1 parent 64157b4 commit ac67b90
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 40 deletions.
19 changes: 1 addition & 18 deletions demo/fullstack-app/src/pages/api/createPoints.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { type NextApiRequest, type NextApiResponse } from 'next'
import { PointsWriter } from '@ceramic-solutions/points'
import { contextWriter, writer } from '@/utils/context'
import type { ModelInstanceDocument } from '@composedb/types'
import { PointsContent, type AggregationContent } from '@/utils/types'
import { fromString } from 'uint8arrays'

const CERAMIC_PRIVATE_KEY: string = process.env.CERAMIC_PRIVATE_KEY ?? ''
const aggregationModelID: string | undefined = process.env.AGGREGATION_ID ?? undefined

interface Request extends NextApiRequest {
body: {
Expand All @@ -23,19 +19,6 @@ interface Response extends NextApiResponse {
export default async function handler(req: Request, res: Response) {
try {
const { recipient, amount, context } = req.body
//eslint-disable-next-line
const seed = fromString(CERAMIC_PRIVATE_KEY, "base16") as Uint8Array;

// create a context writer
const contextWriter = await PointsWriter.fromSeed({
aggregationModelID,
seed,
})

// create a total writer
const writer = await PointsWriter.fromSeed({
seed,
})

// get context aggregation doc if exists
const aggregationDoc: ModelInstanceDocument<AggregationContent> | null =
Expand Down
23 changes: 1 addition & 22 deletions demo/fullstack-app/src/pages/api/readPoints.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { type NextApiRequest, type NextApiResponse } from 'next'
import { getAuthenticatedDID } from '@ceramic-solutions/key-did'
import { PointsReader } from '@ceramic-solutions/points'
import { contextReader, reader } from '@/utils/context'
import type { ModelInstanceDocument } from '@composedb/types'
import { type AggregationContent } from '@/utils/types'
import { fromString } from 'uint8arrays'

const CERAMIC_PRIVATE_KEY: string = process.env.CERAMIC_PRIVATE_KEY ?? ''
const aggregationModelID: string | undefined = process.env.AGGREGATION_ID ?? undefined

interface Request extends NextApiRequest {
body: {
Expand All @@ -23,22 +18,6 @@ interface Response extends NextApiResponse {
export default async function handler(req: Request, res: Response) {
try {
const { recipient, context } = req.body
//eslint-disable-next-line
const seed = fromString(CERAMIC_PRIVATE_KEY, "base16") as Uint8Array;

// generate issuer for reader context
const issuer = await getAuthenticatedDID(seed)

//create a context reader
const contextReader = PointsReader.create({
issuer: issuer.id,
aggregationModelID,
})

//create a total reader
const reader = PointsReader.create({
issuer: issuer.id,
})

// get context aggregation doc if exists
const contextAggregationDoc: ModelInstanceDocument<AggregationContent> | null =
Expand Down
36 changes: 36 additions & 0 deletions demo/fullstack-app/src/utils/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { PointsWriter, PointsReader } from '@ceramic-solutions/points'
import { getAuthenticatedDID } from '@ceramic-solutions/key-did'
import { fromString } from 'uint8arrays'

const CERAMIC_PRIVATE_KEY: string = process.env.CERAMIC_PRIVATE_KEY ?? ''
const aggregationModelID: string | undefined = process.env.AGGREGATION_ID ?? undefined

//eslint-disable-next-line
const seed = fromString(CERAMIC_PRIVATE_KEY, 'base16') as Uint8Array

// create a context writer
const contextWriter = await PointsWriter.fromSeed({
aggregationModelID,
seed,
})

// create a total writer
const writer = await PointsWriter.fromSeed({
seed,
})

// generate issuer for reader context
const issuer = await getAuthenticatedDID(seed)

//create a context reader
const contextReader = PointsReader.create({
issuer: issuer.id,
aggregationModelID,
})

//create a total reader
const reader = PointsReader.create({
issuer: issuer.id,
})

export { contextWriter, writer, contextReader, reader }

0 comments on commit ac67b90

Please sign in to comment.