dimah-surveyv0.5.0
API reference

Configuration

Configure server audiences, browser clients, validation, hooks, and React bindings.

Fill and editor have separate configuration types and share one database. Only fill accepts response validation; only editor accepts publish hooks.

Fill instance

import {
  APIError,
  SURVEY_ERROR_CODES,
  dimahSurvey,
  guardRespondent,
} from "@dimah-survey/server";

import { database } from "@/db";

export const fill = dimahSurvey({
  audience: "fill",
  database,
  guard: async (context) => {
    const session = context.request
      ? await getSession(context.request)
      : null;
    if (!session) {
      throw APIError.from("FORBIDDEN", SURVEY_ERROR_CODES.FORBIDDEN);
    }
    return guardRespondent(session.user.id)(context);
  },
});

Database creates database with @dimah-survey/db. getSession is your application code.

import type { DimahFillConfig } from "@dimah-survey/server";

Prop

Type

The default checkSurveyResult clears values the stored survey cannot assign, runs SurveyJS validation, and returns the survey.data to persist. The server does not fetch choicesByUrl.

import type { ValidateResultInput } from "@dimah-survey/core";

Prop

Type

Return the object to store. Return nothing to keep data. Throw APIError with VALIDATION_FAILED to reject the submit. See Responses.

guard

import type { GuardContext } from "@dimah-survey/core";

Prop

Type

Returning nothing from a fill guard is forbidden. guardRespondent(id) returns { respondentId }. guardAnonymous() opts into capability-based anonymous responses and returns { anonymous: true }.

Lifecycle hooks

import type { FillHooks } from "@dimah-survey/server";

Prop

Type

A resumed start and an idempotent resubmit skip their hooks. on* hooks may abort a write; after* hooks run after storage and cannot roll it back. Keep external side effects retry-safe. HTTP bodies cannot provide hooks.


Editor instance

import {
  APIError,
  SURVEY_ERROR_CODES,
  dimahSurvey,
} from "@dimah-survey/server";

export const editor = dimahSurvey({
  audience: "editor",
  database,
  guard: async ({ request }) => {
    if (!request || !(await isEditor(request))) {
      throw APIError.from("FORBIDDEN", SURVEY_ERROR_CODES.FORBIDDEN);
    }
  },
});

isEditor is your application code. An editor guard returns nothing or throws. Omitting it leaves the editor open to every caller that can reach the route.

import type { DimahEditorConfig } from "@dimah-survey/server";

Prop

Type

import type { EditorHooks } from "@dimah-survey/server";

Prop

Type

onPublish may abort publishing. afterPublish runs after storage. resumeSurvey runs neither hook.


Browser clients

createFillClient and createEditorClient accept the same options. Only the default baseURL differs.

import type { CreateSurveyClientOptions } from "@dimah-survey/react";

Prop

Type

Browser methods take flat objects. fill.api and editor.api take { body } or { query }, plus optional headers or request for guards. See Mount the server.


React

import type { UseSurveyResponseOptions } from "@dimah-survey/react";

Prop

Type

Returned state is documented in React.

import type { UseSurveyDraftOptions } from "@dimah-survey/react";

Prop

Type

The hook does not construct Creator. See Creator.

On this page