Medusa API Field Builder (utils/allowed-fields.ts)

medusa · packages/medusa/src/api/store/utils/allowed-fields.ts · typescript · MIT
database, integration, payments, lang:typescript, tag:medusa, tag:backend

Utility functions to normalize and prefix API response fields for Medusa commerce stores. Use this when managing data access layers in Medusa plugins.

// Source: https://github.com/medusajs/medusa | License: MIT | Copyright 2021 Medusajs
// Provided via justclaw.it.com; keep this notice with the code.
function normalizeField(field: string): string {
  return field.replace(/(^\*|\.\*$)/, "")
}

export function buildAllowedFields(...fieldLists: string[][]): string[] {
  return [...new Set(fieldLists.flat().map(normalizeField))]
}

export function prefixAllowedFields(
  prefix: string,
  ...fieldLists: string[][]
): string[] {
  return buildAllowedFields(...fieldLists).map((field) => `${prefix}.${field}`)
}

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key