Server API

API routes

The server API routes exposed by default are:

  • POST /api/bedita/auth to authenticate user
  • /api/bedita/auth/logout to logout user
  • PATCH /api/bedita/auth/user to update authenticated user attributes as name, surname, etc...
  • POST /api/bedita/auth/reset to request reset user password
  • PATCH /api/bedita/auth/change to change password after reset request
  • POST /api/bedita/auth/optout to delete user
  • GET /api/bedita/* to forward GET requests to BEdita API. See proxyEndpoints configuration
  • POST /api/bedita/* to forward POST requests to BEdita API. See proxyEndpoints configuration
  • PATCH /api/bedita/* to forward PATCH requests to BEdita API. See proxyEndpoints configuration
  • DELETE /api/bedita/* to forward DELETE requests to BEdita API. See proxyEndpoints configuration
  • POST /api/bedita/_project setup the project used from BEdita client. See projects configurtaion

Utils

There are some utils functions auto imported in server context.

beditaApiClient

beditaApiClient(event: H3Event): Promise<BEditaApiClient>

It resolves with the BEdita API client from @atlasconsulting/bedita-sdk.

/server/api/example.ts
export default defineEventHandler(async (event) => {
  const client = await beditaApiClient(event);
  const response = await client.get('/documents');

  return response.data;
});

handleBeditaApiError

handleBeditaApiError(event: H3Event, error: AxiosError | H3Error | any): Promise<ApiResponseBodyError>

It helps to handle BEdita API errors setting response status and payload.

/server/api/example.ts
export default defineEventHandler(async (event) => {
  try {
    const client = await beditaApiClient(event);
    const response = await client.get('/documents');

    return response.data;
  } catch (error) {
    return handleBeditaApiError(event, error);
  }
});

getProjectConfig

getProjectConfig(sessionData: SessionData, config: RuntimeConfig, property?: keyof BeditaProjectConf)

Return the configuration properties reading merged from current project and core properties.

recaptchaVerifyToken

recaptchaVerifyToken(token: string, action: string, throwError = true): Promise<boolean>

It needs to verify recapctha token sent by client. By default it throws a 400 Bad Request if token validation fails.

/server/api/example.ts
export default defineEventHandler(async (event) => {
  const body = await readBody(event);
  await recaptchaVerifyToken(body?.recaptcha_token, 'my-action');
  // do other stuff
});

getSessionConfig

getSessionConfig(): SessionConfig

Get the actual session configuration. It is useful to retrieve session.

/server/api/example.ts
export default defineEventHandler(async (event) => {
  const session = await useSession(event, getSessionConfig());
  // do stuff with session
});

filterUserDataToStore

filterUserDataToStore(data: UserAuth): UserDataStore

Filter user data and return a minimum data. Used to store in session cookie a miminal set of data.

/server/api/example.ts
export default defineEventHandler(async (event) => {
  try {
    const client = await beditaApiClient(event);
    const response = await client.get('/auth/user');

    return filterUserDataToStore(response?.data);
  } catch (error) {
    return handleBeditaApiError(event, error);
  }
});