Socket
Book a DemoInstallSign in
Socket

@lcdev/api-fields

Package Overview
Dependencies
Maintainers
3
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@lcdev/api-fields

Trim fields from your models before returning in API

0.1.5
latest
Source
npmnpm
Version published
Weekly downloads
9
28.57%
Maintainers
3
Weekly downloads
 
Created
Source

API Fields

Licensed under MPL 2.0 Build Status npm BundlePhobia

A small utility package that enables an easy way to guarantee that your API doesn't return fields that you didn't want it to.

yarn add @lcdev/api-fields@0.1

You might want to reduce the duplication when extracting return values. Most of the time, you want to return the same fields for the same entities, records, etc.

API Fields is a decorator for classes that gives you the ability to tie in to @lcdev/mapper, specifically its extract function.

import { ApiField } from '@lcdev/api-fields';

class User extends BaseEntity {
  @ApiField()
  id: number;

  // we never want to give this back in API responses
  // maybe it's private, or maybe we don't want consumers to depend on it
  privateField: number;

  @ApiField()
  firstName: string;

  // here, we only want the API Fields of Permission in the nested field
  @ApiField(() => Permission)
  permission: Permission;

  ...
}

To reveal the 'Extraction' object that can be used by @lcdev/mapper:

import { getApiFields } from '@lcdev/api-fields';
import { extract } from '@lcdev/mapper';

// getApiFields can be called anywhere to retrieve the `Extraction` object
const extraction = getApiFields(User);

// use the mapper package to take back only the fields you're interested in
const trimmedFields = extract(fullFields, extraction);

Decorator possibilities:

  • @ApiField() property means take all of property
  • @ApiField(() => PropertyType) property means take ApiFields of property
  • @ApiField(() => [PropertyType]) property[] means take ApiFields of all propertys
  • @ApiField({ ... }) property means take { ... } from property

You might want to create middleware in your router to do this type of extraction for you. Internally at Launchcode we do just that, and would like to open-source that effort as well.

Alternatives

FAQs

Package last updated on 26 Apr 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.