Skip to content

Library

The Library is your collection of file blueprints. A Blueprint is a special type of file that uses some Codedraw defined variables. Those variables are interpolated when your project is compiled to code. A file in your library can be of type HANDLER, MIDDLEWARE, ENTRY_POINT.

Handler

You can create several type of handler blueprints. Some can be just empty, others can be more specific. For instance here is what an empty handler blueprint looks like:

typescript
import { Environment, Variables } from '@/context'
import { Context } from 'hono'
import { EndpointHandler, HonoApp, Responses } from '@coderpunktech/codedraw-core'

export const $functionName$: EndpointHandler<Environment, Variables> = async (
  _context: Context<HonoApp<Environment, Variables>>
): Promise<Response> => {
  // your code here
  return Responses.METHOD_NOT_ALLOWED('Not implemented')
}

Handlers must have the $functionName$ variable as the name of the function.

Here is an example of a more complex blueprint that reads a document from a MongoDb Collection:

typescript
import { Environment, Variables } from '@/context'
import { Context } from 'hono'
import {
  EndpointHandler,
  HonoApp,
  MongoDBArguments,
  ResponseError,
  Responses,
  useDocumentService,
} from '@coderpunktech/codedraw-core'

export const $functionName$: EndpointHandler<Environment, Variables> = async (
  _context: Context<HonoApp<Environment, Variables>>,
): Promise<Response> => {
  const documentService = useDocumentService<Environment, Variables, MongoDBArguments>(_context)
  const id = _context.req.param('id')

  if (!id) {
    throw new ResponseError(400, 'Bad Request')
  }

  const document = await documentService.getDocument({
    collectionName: '<your-collection-name>',
    query: {
      filter: { _id: id },
    },
  })

  if (!document) {
    throw new ResponseError(404, 'Not Found')
  }

  return Responses.OK(document)
}

Here is another example of an handler that stores a file into an R2 bucket:

typescript
import { Environment, Variables } from '@/context'
import { Context } from 'hono'
import { EndpointHandler, HonoApp, R2Arguments, Responses, useDocumentService } from '@coderpunktech/codedraw-core'

export const ApiV1BlueprintAllGet: EndpointHandler<Environment, Variables> = async (
  _context: Context<HonoApp<Environment, Variables>>,
): Promise<Response> => {
  const documentService = useDocumentService<Environment, Variables, R2Arguments>(_context)
  const payload = await _context.req.json()
  const blueprints = await documentService.createDocument<any>(payload, {
    key: payload.id,
  })
  return Responses.OK(blueprints)
}

Entry Point

The entry point is a file template that's used for worker to worker dependencies using an RPC strategy.

Entry points must have the $bindingEntryPoint$ as the class name.

Editor

Curating your own blueprints collection allows you to then select them when you are adding a Custom handler to a worker in the Project Dashboard. Blueprints content are managed using the blueprint editor. Here is what it looks like:

/codedraw/library_editor.png

The editor is divided into two columns. On the left your tools drawer and on the right the content editor. You can browse the available variables as well as adding npm dependencies. You can name, tag and add a description to your blueprint to better find it later on.