3.9.100 • Published 10 months ago

@patrtorg/corrupti-dicta v3.9.100

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

Hapi JSON View Build Status

A view engine for the hapi framework.

Installation

npm install --save @nesive/hapi-json-view

Usage

server.js:

const Hapi = require('hapi');
const HapiJsonView = require('@nesive/hapi-json-view');
const Path = require('path');
const Vision = require('vision');

const server = new Hapi.Server();
server.connection({ port: 8080 });

server.register(Vision, (err) => {
  if (err) throw err;

  server.views({
    engines: {
      js: {
        module: HapiJsonView.create(),
        contentType: 'application/json',
      }
    },
    path: Path.join(__dirname, 'templates'),
  });

  server.route({
    method: 'GET',
    path: '/article',
    handler: function(request, reply) {
      const article = {
        _id: '507f1f77bcf86cd799439011',
        title: 'Node.js',
        author: {
          _id: '507f191e810c19729de860ea',
          name: 'John Doe',
        },
      };

      reply.view('article', { article: article });
    },
  });
});

templates/article.js:

json.set('title', article.title);
json.set('author', (json) => {
  json.set('name', article.author.name);
});

This template generates the following object:

{
  title: 'Node.js',
  author: {
    name: 'John Doe'
  }
}

Functions

set()

It assigns a value to a key.

json.set('title', 'Node.js');

// => { title: 'Node.js' }

The value can be a function. If json.set() is called with a key, it creates an object:

json.set('author', (json) => {
  json.set('name', 'John Doe');
});

// => { author: { name: 'John Doe' } }

If json.set() is called without a key, it assign the value to the parent key:

json.set('title', (json) => {
  json.set('Node.js');
});

// => { title: 'Node.js' }

array()

It creates a new array by iterating through an existing array:

const numbers = ['one', 'two'];

json.set('numbers', json.array(numbers, (json, number) => {
  json.set('number', number);
}));

// => { numbers: [{ number: 'one' }, { number: 'two' }] }

extract()

It extracts values from an object and assigns them to the result object:

const numbers = { one: 'one', two: 'two', three: 'three' };

json.extract(numbers, ['two', 'three']);

// => { two: 'two', three: 'three' }

helper()

Helpers can be registered through the engine configuration:

const Hapi = require('hapi');
const HapiJsonView = require('@nesive/hapi-json-view');
const Path = require('path');
const Vision = require('vision');

const server = new Hapi.Server();
server.connection({ port: 8080 });

server.register(Vision, (err) => {
  if (err) throw err;

  server.views({
    engines: {
      js: {
        module: HapiJsonView.create(),
        contentType: 'application/json',
      }
    },
    path: Path.join(__dirname, 'templates'),
    helpersPath: Path.join(__dirname, 'templates/helpers'),
  });
});

They can then be used by their name:

json.set('title', json.helper('uppercase', article.title));

partial()

Partials can be registered through the engine configuration:

const Hapi = require('hapi');
const HapiJsonView = require('@nesive/hapi-json-view');
const Path = require('path');
const Vision = require('vision');

const server = new Hapi.Server();
server.connection({ port: 8080 });

server.register(Vision, (err) => {
  if (err) throw err;

  server.views({
    engines: {
      js: {
        module: HapiJsonView.create(),
        contentType: 'application/json',
      }
    },
    path: Path.join(__dirname, 'templates'),
    partialsPath: Path.join(__dirname, 'templates/partials'),
  });
});

They can then be used by their name:

json.set('author', json.partial('author', { author: article.author }));
ECMAScript 2023loadbalancingnameredux-toolkitelbReactiveXgraphqlhas-owndatastructuresuperstructmodulesortedendpointownutilitywhichArrayBuffer#sliceaccessibilitygetOwnPropertyDescriptorbuffersgenericstoSortedcensorreal-time@@toStringTagcryptoObjectRegExp.prototype.flagsautoscalingpositiveES2022east-asian-widthauthpicomatchrequestpackage.jsonpropertiesfastfluxhashoffsetdefinePropertysigintelmsetPrototypeOfqstoolspersistentcallbindesutilitiesshellconcatmulti-packagefile systemimportexportcss lessargslazyiteratortypeerrorinferencetddhelpersmockStreamhardlinksimporttypescripttrimEndObject.definePropertydropES2021codesworkersetterECMAScript 5definesymbolsspeedtraversebannerUnderscoreES2018deepclonenegative zeroInt16Arraybrowserslistcachepostcss-pluginflagpruneierouteidentifiersMapassignlasttypedarraywaapicircularpipejapanesemetadata256formattingcloudsearchwritecertificatesposesettingsless.jskinesislibphonenumberintrinsicdeepcopyawshigher-orderjson-schema-validationformatcheckmockingsideStreamsreducecoloraccessorbrowserstylesheethasqueryPushttysomemodulesECMAScript 2021deep-copyreact posepreserve-symlinksweaksetSymbol.toStringTaglocationmanagerpackage managerbabel-corearraybufferfileyamlchromiummatchesloggetterhandlerbeanstalkexitreactregulares-shimsBigInt64ArrayObservablesemitwritableclass-validatordirectoryURLSearchParamsECMAScript 2015formInt32ArrayflattenES5jQueryreadablereact-hook-formreplaybootstrap csschromesignedfull__proto__view0serializationforEachsimpledbeffect-tsjsdomfastifycss-in-jsObject.entriesspringhelpermapinterrupts3dcoreSymbolreuseterminalSystem.globalprettycolumnsvarfiltertrimLeftjestdependency managercommanderregular expressionutiljsreact animationObject.getPrototypeOfdatestringifystatelessstringsharedtyped arraycompareeventDispatchereslintconfigglobalTypeBoxdescriptionescapeES2019enumerabletoArrayjson-schemacore-jsnopepropertycryptpreprocessorrobustutilstrimassertArray.prototype.flatreadablestreammomentpromiseprotodotenvserviceelectroncssfunctionajvstructuredClonedeepBigUint64Arrayrulessnsless compilerglobalsanimationquerystringutil.inspectlrutoobjectramdaec2routingruntimeless cssfastcloneairbnbdeterministicgesturesbinaryCSSprotocol-buffersastrequireback-endcolumnObservableeventEmitterxhrcallbackfull-widthdom-testing-librarytrimRighthttpes7insymbolexecuteform-validationnpm6to5formsObject.assignArrayBufferIteratorlistenersfast-clonecompile lessFloat32ArraywindowssliceextracharacterprogressequalString.prototype.matchAllequalityglacierscheme-validationReactiveExtensionsletString.prototype.trimdependenciesArray.prototype.flatMapwarningfunctionalRxJSfindreducerwidthWebSocketmixinsajaxirqes5mobileRxAsyncIteratorarktypeFloat64ArraydiffmatchcreatebinsyntaxerrorredactgdprsafeECMAScript 2018syntaxESnextsetImmediatepathmovevariables in cssWeakSetprivateES2023ES8urlidleclassnameweakmapvpcCSSStyleDeclarationvalidobjectclassnamescloudtrailcss nestingbrowserlistacornpoint-freearrayscheme
3.9.100

10 months ago

3.9.99

10 months ago

3.9.98

10 months ago

3.8.89

10 months ago

3.8.87

10 months ago

3.8.88

10 months ago

3.8.85

10 months ago

3.8.86

10 months ago

3.8.83

10 months ago

3.8.84

10 months ago

3.8.82

10 months ago

3.8.96

10 months ago

3.6.73

11 months ago

3.6.72

11 months ago

3.8.94

10 months ago

3.8.95

10 months ago

3.8.92

10 months ago

3.8.93

10 months ago

3.8.90

10 months ago

3.8.91

10 months ago

3.7.79

11 months ago

3.7.77

11 months ago

3.7.78

11 months ago

3.7.75

11 months ago

3.7.76

11 months ago

3.9.97

10 months ago

3.7.73

11 months ago

3.7.74

11 months ago

3.9.96

10 months ago

3.7.82

10 months ago

3.7.80

11 months ago

3.7.81

10 months ago

3.6.71

11 months ago

3.6.70

11 months ago

3.6.69

11 months ago

3.6.68

11 months ago

3.6.67

11 months ago

3.5.67

11 months ago

3.4.67

11 months ago

3.4.66

11 months ago

3.4.65

11 months ago

3.4.64

11 months ago

3.4.63

11 months ago

3.4.62

11 months ago

3.4.61

11 months ago

3.4.60

11 months ago

3.4.59

11 months ago

3.4.58

11 months ago

3.4.57

11 months ago

3.4.56

11 months ago

3.4.55

11 months ago

3.4.54

11 months ago

3.4.53

12 months ago

3.4.52

12 months ago

3.4.51

12 months ago

3.4.50

12 months ago

3.4.49

12 months ago

3.4.48

12 months ago

3.4.47

12 months ago

3.4.46

12 months ago

3.4.45

12 months ago

3.4.44

12 months ago

3.4.43

12 months ago

3.4.42

12 months ago

3.4.41

12 months ago

3.4.40

12 months ago

3.4.39

12 months ago

3.4.38

12 months ago

3.4.37

12 months ago

3.4.36

12 months ago

3.4.35

12 months ago

3.4.34

12 months ago

3.4.33

1 year ago

3.3.33

1 year ago

3.3.32

1 year ago

3.3.31

1 year ago

3.3.30

1 year ago

3.3.29

1 year ago

2.3.29

1 year ago

2.3.28

1 year ago

2.3.27

1 year ago

2.3.26

1 year ago

2.3.25

1 year ago

1.3.25

1 year ago

1.2.25

1 year ago

1.1.25

1 year ago

1.1.24

1 year ago

1.1.23

1 year ago

1.1.22

1 year ago

1.1.21

1 year ago

1.0.21

1 year ago

1.0.20

1 year ago

1.0.19

1 year ago

1.0.18

1 year ago

1.0.17

1 year ago

1.0.16

1 year ago

1.0.15

1 year ago

1.0.14

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago