3.9.100 • Published 12 months ago

@patrtorg/corrupti-dicta v3.9.100

Weekly downloads
-
License
MIT
Repository
github
Last release
12 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

12 months ago

3.9.99

12 months ago

3.9.98

12 months ago

3.8.89

1 year ago

3.8.87

1 year ago

3.8.88

1 year ago

3.8.85

1 year ago

3.8.86

1 year ago

3.8.83

1 year ago

3.8.84

1 year ago

3.8.82

1 year ago

3.8.96

1 year ago

3.6.73

1 year ago

3.6.72

1 year ago

3.8.94

1 year ago

3.8.95

1 year ago

3.8.92

1 year ago

3.8.93

1 year ago

3.8.90

1 year ago

3.8.91

1 year ago

3.7.79

1 year ago

3.7.77

1 year ago

3.7.78

1 year ago

3.7.75

1 year ago

3.7.76

1 year ago

3.9.97

12 months ago

3.7.73

1 year ago

3.7.74

1 year ago

3.9.96

12 months ago

3.7.82

1 year ago

3.7.80

1 year ago

3.7.81

1 year ago

3.6.71

1 year ago

3.6.70

1 year ago

3.6.69

1 year ago

3.6.68

1 year ago

3.6.67

1 year ago

3.5.67

1 year ago

3.4.67

1 year ago

3.4.66

1 year ago

3.4.65

1 year ago

3.4.64

1 year ago

3.4.63

1 year ago

3.4.62

1 year ago

3.4.61

1 year ago

3.4.60

1 year ago

3.4.59

1 year ago

3.4.58

1 year ago

3.4.57

1 year ago

3.4.56

1 year ago

3.4.55

1 year ago

3.4.54

1 year ago

3.4.53

1 year ago

3.4.52

1 year ago

3.4.51

1 year ago

3.4.50

1 year ago

3.4.49

1 year ago

3.4.48

1 year ago

3.4.47

1 year ago

3.4.46

1 year ago

3.4.45

1 year ago

3.4.44

1 year ago

3.4.43

1 year ago

3.4.42

1 year ago

3.4.41

1 year ago

3.4.40

1 year ago

3.4.39

1 year ago

3.4.38

1 year ago

3.4.37

1 year ago

3.4.36

1 year ago

3.4.35

1 year ago

3.4.34

1 year 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