{"id":19419065,"url":"https://github.com/launchcodedev/api-fields","last_synced_at":"2026-02-26T06:09:06.651Z","repository":{"id":42866202,"uuid":"259139847","full_name":"launchcodedev/api-fields","owner":"launchcodedev","description":"Class decorator to trim object fields in API responses","archived":false,"fork":false,"pushed_at":"2023-01-06T04:28:15.000Z","size":1113,"stargazers_count":3,"open_issues_count":10,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-10-26T16:58:41.602Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/@lcdev/api-fields","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/launchcodedev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-04-26T21:45:59.000Z","updated_at":"2020-12-12T16:06:09.000Z","dependencies_parsed_at":"2023-02-05T07:31:26.322Z","dependency_job_id":null,"html_url":"https://github.com/launchcodedev/api-fields","commit_stats":null,"previous_names":["servall/api-fields"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/launchcodedev/api-fields","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/launchcodedev%2Fapi-fields","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/launchcodedev%2Fapi-fields/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/launchcodedev%2Fapi-fields/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/launchcodedev%2Fapi-fields/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/launchcodedev","download_url":"https://codeload.github.com/launchcodedev/api-fields/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/launchcodedev%2Fapi-fields/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29849833,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-25T22:37:40.667Z","status":"online","status_checked_at":"2026-02-26T02:00:06.774Z","response_time":89,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-10T13:16:02.887Z","updated_at":"2026-02-26T06:09:06.618Z","avatar_url":"https://github.com/launchcodedev.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# API Fields\n[![Licensed under MPL 2.0](https://img.shields.io/badge/license-MPL_2.0-green.svg)](https://www.mozilla.org/en-US/MPL/2.0/)\n[![Build Status](https://github.com/launchcodedev/api-fields/workflows/CI/badge.svg)](https://github.com/launchcodedev/api-fields/actions)\n[![npm](https://img.shields.io/npm/v/@lcdev/api-fields.svg)](https://www.npmjs.com/package/@lcdev/api-fields)\n[![BundlePhobia](https://badgen.net/bundlephobia/minzip/@lcdev/api-fields)](https://bundlephobia.com/result?p=@lcdev/api-fields@latest)\n\nA small utility package that enables an easy way to guarantee that your API doesn't return fields\nthat you didn't want it to.\n\n```bash\nyarn add @lcdev/api-fields@0.1\n```\n\nYou might want to reduce the duplication when extracting return values. Most of the time,\nyou want to return the same fields for the same entities, records, etc.\n\nAPI Fields is a decorator for classes that gives you the ability to tie in to [`@lcdev/mapper`](https://github.com/launchcodedev/mapper),\nspecifically its `extract` function.\n\n```typescript\nimport { ApiField } from '@lcdev/api-fields';\n\nclass User extends BaseEntity {\n  @ApiField()\n  id: number;\n\n  // we never want to give this back in API responses\n  // maybe it's private, or maybe we don't want consumers to depend on it\n  firstName: string;\n  lastName: string;\n\n  @ApiField() // works just fine on getters\n  get fullName() {\n    return `${this.firstName} ${this.lastName}`;\n  }\n\n  // here, we only want the API Fields of Permission in the nested field\n  @ApiField(() =\u003e Permission)\n  permission: Permission;\n\n  ...\n}\n```\n\nUnder the hood, this creates a listing of the fields you want to expose. We\ncall them \"API Fields\" because this is usually the way you expose fields in\nJSON API responses.\n\nWe can get that metadata about any given class with the `getAPIFields` function.\nThe object returned can actually be used directly in `@lcdev/mapper`.\n\n```typescript\nimport { getApiFields } from '@lcdev/api-fields';\nimport { extract } from '@lcdev/mapper';\n\n// getApiFields can be called anywhere to retrieve the `Extraction` object\nconst extraction = getApiFields(User);\n\n// use the mapper package to take back only the fields you're interested in\nconst trimmedFields = extract(fullFields, extraction);\n```\n\n### Formats\n- `@ApiField() propName`: extract `propName` as-is\n- `@ApiField(() =\u003e PropertyType) propName`: extract the ApiFields of `PropertyType` as `propName`\n- `@ApiField(() =\u003e [PropertyType]) propName[]`: map as array, extracting ApiFields of each element\n- `@ApiField({ ... }) propName`: extract fields from `propName` (same as `@lcdev/mapper`)\n- `@ApiField(false) propName`: don't include this field\n\n### Renames\nRenaming a field is supported, in the same way it is in `@lcdev/mapper`.\n\n```typescript\nimport { ApiField } from '@lcdev/api-fields';\nimport { rename } from '@lcdev/mapper';\n\nclass ImageUpload {\n  @ApiField(rename('url'))\n  awsURL: string;\n}\n```\n\nWhen being extracted, the field will be renamed.\n\n### Transforms\nTransforming a field is supported, in the same way it is in `@lcdev/mapper`.\n\n```typescript\nimport { ApiField } from '@lcdev/api-fields';\nimport { transform } from '@lcdev/mapper';\n\nclass ImageUpload {\n  @ApiField(transform(v =\u003e v.replace('https:', '')))\n  awsURL: string;\n}\n```\n\n### Alternatives\n- [class-transformer](https://github.com/typestack/class-transformer)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flaunchcodedev%2Fapi-fields","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flaunchcodedev%2Fapi-fields","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flaunchcodedev%2Fapi-fields/lists"}