New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

binary-serializer-next

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

binary-serializer-next

--- Next gen Class hydration library, super slim ~1Kb. Written mostly for Typescript. Works in both node and browser. While it is possible to use with plain JS it will require some pre-processor like babel with decorators support.

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

Hydrator Next

Next gen Class hydration library, super slim ~1Kb. Written mostly for Typescript. Works in both node and browser. While it is possible to use with plain JS it will require some pre-processor like babel with decorators support.

Only depends on reflect-metadata polyfil.

For Typescript, you need to have these two options turned on in tsconfig.json:

{
	"compilerOptions": {
		"emitDecoratorMetadata": true,
		"experimentalDecorators": true
	}
}

npm

WARNING: API is not yet final

Requirements

Technically it should have no requirements.

Docs

Read the docs here

Installation

For yarn:

yarn add hydrator-next

For npm:

npm i hydrator-next

For browser just include the script or import it via ES6 import statement.

Some examples

Imports:

// ES6 JS/Typescript style
import { Hydrator } from 'hydrator-next';

// require

const { Hydrator } = require('hydrator-next');

Basic usage:

class Test {
	some = 1;
	property = 'a';
}

// hydrate from raw data
const obj = Hydrator.hydrate(Test, {some: 2, property: 'b'});

FieldType options (all of them are optional):

PropertyTypeDescription
typestringProperty type, has to be registered with Converter.registerConverter
nestedconstructorIf property is another nested class, explicitly state the class, ex. Date
arraybooleanIs property an array, need to also provide type otherwise it will have no effect
converter(value: any, direction: DataDirection, type: string) => any;custom converter function, example of usage below
translatestringShould this property be translated into some other field when hydrating/dehydrating, useful for mapping plain object under_score_case to camelCaseProperty
translateDatabasestringSame as translate however will only have effect when hydrating/dehydrating to database
ignoreDatabasebooleanvalue will be ignored when coming or going to database, which basically means field does not exist in database
ignorePlainbooleanvalue will be ignored when coming or going to plain object, which basically means field should not be hydrated while operating on raw data
ignorebooleanvalue will be ignored, won't ever be touched by Hydrator

Advanced usage:

Converter.registerConverter('string', convertString);
Converter.registerConverter('number', convertNumber);
Converter.registerConverter('bigint', convertBigInt);

function customConverter(value: any, direction: DataDirection, type: string) {
	switch (direction) {
		case DataDirection.FromDatabase:
		case DataDirection.FromPlain:
			return '_' + value;
		case DataDirection.ToDatabase:
		case DataDirection.ToPlain:
			return value.substr(1);
	}
}

class TestClassCustom {
	@FieldType({ converter: customConverter })
	cust: any;
	
	// Forced to string upon hydration
	@FieldType({ type: 'string' })
	str: string;
	
	// Forced to number upon hydration
	@FieldType({ type: 'number' })
	num: string;

	// Forced to BigInt upon hydration
	@FieldType({ type: 'bigint' })
	bi: string;
	
	// Any data will come here
	fieldOfAnyValue: any = null;

	// Will not be changed at all upon migration
	@FieldType({ ignore: true })
	ignorethis: string = '9';
}

Hydrator.hydrate(TestClassCustom, {
	cust: 'a',
	str: 123,
	num: '999',
	bi: 123123,
	fieldOfAnyValue: new Date(),
	// field is not decorated nor present as property in class, thus will be ignored.
	willBeIgnored: 'aaa',
	ignorethis: 'nope'
})

Keywords

class

FAQs

Package last updated on 27 May 2023

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