hapi-view-models

Role-filtered view model support for Hapi

hapi View Models

A plugin to provide a concept of 'view-models' to hapi.

Purpose

When rendering payloads from an API, we often want to provide different subsets (or supersets) of data to users with different roles, or scopes. By leveraging lodash.omit and server.decorate, we can contain the complexity of doing this as a fairly neat abstraction, reducing the overall complexity of payload modification and filtering, and keeping our controllers clean.

With hapi-view-models it is possible to render a variety of different versions of a single entity (payload) from a single endpoint.

Installing

Installing is done in the usual way

--- ------- ----------------

Usage

The vm reply helper allows you to render views of your data:

----- ---------------- - --------------
--------------
  ---
  ------- --------- ------ -
    -------------------------- ---------
  -
--

Where vm takes the KeyPairViewModel and filters the data inside response according to the user's scope. Response can be a single entity or an array of entities.

If you're returning a response envelope you can provide a path to the entity as a third argument to the vm reply helper.

----- ---------------- - --------------
--------------
  ---
  ------- --------- ------ -
    -------------------------- -
      ------- -
        ----- -----
      -
    -- --------------
  -
--

Real World Example

Suppose we owned a cryptocurrency exchange. That's very 'of the moment', isn't it?

Lets say our wallet owner has a role of 'owner', and a visitor doesn't have this role.

We'd want to build a view model to render a wallet's data in a secure way. We declare a set of properties that are included in the payload for each role. Including a property in a role automatically excludes that data from all other roles.

Roles are defined by hapi in request.auth.credentials.scope and are controlled by your auth mechanism.

Our view model extends ViewModel and looks like this:

----- - --------- - - ---------------------------

----- ---------------- ------- --------- -
    --- -------- -- -
      ------ -
        ------ -----------
      -
    -
-

And our handler would look something like this:

----- - ------ - - ---------------------------
----- ---------------- - --------------------------------------

-- -------- --- ------ ---- ----
----------------------- --- -- -
  -------------------
--

-------- ------------- --------- -
  -- ------- ---- - --------- ---- ---- -------
  ----- ------- - -
    -------- --------
    ------- -------
  -
-

--------------
  ------- ------
  ----- --------------------
  -------- -------- --------- ------ -
    ----- ------- - -------------------------------------
    -------------------------- --------
  -
--

The rendered data would look something like this:

-- -- --- ----- -------- --- ------
-
  -------- --------
  ------- -------
-

-- --- -- - ------- -------- --- ------
-
  ------- -------
-

Understanding get includes()

The getter method get includes() or just .includes as a property on your view model is a mapping of scope to an array of properties that are (deep) filtered from the resultant payload. This means we also support nesting!

This means you can do:

----- ---- - -
  -- -
    ---- -----
  --
  -- -
    -- ------ ------
    -- -
      -- -----------
    -
  -
-

----- ------------- ------- --------- -
    --- -------- -- -
      ------ -
        ------ ------ -- ---- - --- --- -------- ---
        ------ ----- ---- -- ---- - --- --- -------- --- --- ---
        ------ --------- -- ------- ---- ----- --- --- --- - --- ---- --- --- -------- -- --- ---- -- ---- -- ------- -- -------
      -
    -
-

You'll notice something going on here with includes. Includes is an 'exclusive' mapping. This means that if you declare a property visible by a role, it is hidden for all other roles. This means that you can minimally hide role-sensitive data, without having to re-iterate yourself or risk the leak of private properties leaking through.

TL;DR: Once a property is declared as visible to a role, it is automatically invisible to all other roles.

Ideally your user should have all the roles it needs to see all the data it needs, but if you like, you can 're-declare' visibility as we have done in the ase of role2 above. A user wanting to see a can have roles role1, role2, or both. a (and as a result a.foo) isn't visible to any other roles.

Structure

The plugin exports two modules:

  • plugin which is the hapi plugin providing reply.vm()
  • ViewModel which is the base class your view models should extend.

The plugin uses a slightly stricter extension of standard-style

Contributing

To contribute to the plugin, fork it to your own github account, create a branch, make your changes, and submit a PR.

Note that Vendigo Finance Ltd requires that you include tests to cover your new code, along with your PR in order to get it merged.

To run our test suite:

--- -------
--- ----
--- --- ----

HomePage

https://github.com/vendigo-group/hapi-view-models#readme

Repository

git+https://github.com/vendigo-group/hapi-view-models.git

来源:JavaScript中文网 ,转载请联系管理员! 本文地址:https://www.javascriptcn.com/post/60056c7581e8991b448e5f23


猜你喜欢

  • GraphQL:用 Connection 优化节点查询

    前言 GraphQL 是一种由 Facebook 开发的数据查询和操作语言,它提供了一种更高效、更灵活的方式来获取和操作数据。GraphQL 的一个重要特性就是可以精确地指定需要查询的数据,避免了传统...

    2 个月前
  • Server-sent Events 的浏览器支持情况及解决方法

    什么是 Server-sent Events? Server-sent Events(简称 SSE)是一种基于 HTTP 的服务器推送技术,它可以让服务器向客户端发送事件流,客户端通过监听这个事件流来...

    2 个月前
  • ECMAScript 2020(ES11)中的新特性:BigInt 转换

    在 ECMAScript 2020(ES11)中,新增了一种数据类型:BigInt。它是一种可以表示任意大整数的数据类型,可以用来解决 JavaScript 中整数运算的精度问题。

    2 个月前
  • CSS Reset 在 IE6、IE7 等老浏览器中的应用

    什么是 CSS Reset CSS Reset 是一种通过重置浏览器默认样式的方式,消除不同浏览器之间的差异,从而实现更加一致的样式效果的技术手段。在前端开发中,使用 CSS Reset 可以让我们更...

    2 个月前
  • ES6 中的类继承和原型链之间的关系解析

    在 ES6 中,引入了 class 关键字,使得 JavaScript 也具备了面向对象编程的能力。在类继承和原型链之间,有着密切的关系。本文将详细解析 ES6 中的类继承和原型链之间的关系,并提供一...

    2 个月前
  • 如何使用 Redux 处理 React 应用中的表单数据

    前言 在开发 React 应用时,表单数据的处理是非常常见的需求。然而,由于 React 的单向数据流和组件化特性,传统的表单处理方式可能会变得非常繁琐。而 Redux 作为一种状态管理工具,可以帮助...

    2 个月前
  • Redis 处理高并发的策略

    前言 随着互联网的发展,高并发已经成为了一个不可避免的问题。而 Redis 作为一款高性能的 NoSQL 数据库,也成为了处理高并发的重要工具之一。本文将会介绍 Redis 处理高并发的策略,并且会提...

    2 个月前
  • 响应式设计中的图片适配问题解决方案

    在响应式设计中,图片适配是一个比较棘手的问题。如果不加以处理,可能会导致图片在不同设备上显示不佳,影响用户体验。本文将介绍响应式设计中的图片适配问题,并提供解决方案。

    2 个月前
  • 解析 TypeScript 中 encapsulation(封装)的实现方式

    解析 TypeScript 中 encapsulation(封装)的实现方式 在 TypeScript 中,封装(encapsulation)是一种重要的面向对象编程的特性。

    2 个月前
  • PM2 崩溃处理:如何避免由于 PM2 进程奔溃导致应用崩溃?

    在前端开发中,我们经常使用 PM2 进行进程管理和部署。但是,当 PM2 进程崩溃时,应用也会跟着崩溃。如何避免这种情况的发生?本文将介绍 PM2 崩溃处理的方法和技巧,帮助您更好地管理和部署应用。

    2 个月前
  • 在 Node.js 中运行 HTTPS 服务器的方法

    Node.js 是一个非常流行的 JavaScript 运行时环境,它可以让我们通过 JavaScript 编写服务器端应用程序。在开发 Web 应用程序时,安全性是非常重要的。

    2 个月前
  • 详解 ECMAScript 2018 中的三个新操作符及其用法

    ECMAScript 2018 (简称 ES2018) 是 JavaScript 语言的最新标准,其中包含了许多新特性和语法糖。本文将详细介绍其中的三个新操作符及其用法,分别是:扩展运算符、剩余运算符...

    2 个月前
  • 解决 Enzyme 测试 React Native 组件时动画无法渲染的问题

    在开发 React Native 应用时,我们经常需要使用 Enzyme 来测试组件。然而,当我们测试涉及到动画的组件时,我们可能会遇到一些问题:动画无法渲染,导致测试失败。

    2 个月前
  • 使用 React Router 打造复杂而强大的 SPA 应用

    随着 Web 技术的不断发展,单页应用(Single Page Application,SPA)已经成为了现代 Web 应用的主流。SPA 通过异步加载数据和动态更新页面,提供了更快速、更流畅的用户体...

    2 个月前
  • AngularJS 中如何使用 ng-repeat 中的 filter 来过滤数据

    在 AngularJS 中,ng-repeat 指令是用于循环遍历数组或对象并生成 HTML 元素的常用指令。而 ng-repeat 指令中的 filter 属性则是用于过滤数据的功能。

    2 个月前
  • 如何在 Chai 中验证 Promise.all

    如何在 Chai 中验证 Promise.all 在前端开发中,Promise.all 是一个非常常用的功能,它可以让我们在多个异步操作完成后再执行一些操作,这个功能在实际开发中非常实用。

    2 个月前
  • Mongoose 实现数据批量更新的方式详解

    前言 在前端开发中,经常会涉及到对数据库中的数据进行批量更新的操作。而 Mongoose 是一款 Node.js 平台下的 MongoDB 对象模型工具,它提供了一种方便的方式来操作 MongoDB ...

    2 个月前
  • 在使用 lit-element 的时候,如何解决麻烦的 Shadow DOM 的变量传递问题

    前言 在使用 Web Components 的时候,我们通常会使用 Shadow DOM 来实现封装和样式隔离。然而,Shadow DOM 的封闭性也带来了一些挑战,其中之一就是变量传递问题。

    2 个月前
  • Tailwind CSS 如何实现动态换肤?

    随着互联网的发展,越来越多的网站和应用开始支持动态换肤功能。动态换肤不仅可以提升用户体验,还可以让用户在不同的环境下选择适合自己的主题,增加用户黏性和满意度。本文将介绍如何使用 Tailwind CS...

    2 个月前
  • 如何在 Less 中使用字符串操作函数?

    在前端开发中,样式表是不可或缺的一部分。而 Less 是一种动态样式语言,它是 CSS 的一种扩展。在 Less 中,我们可以使用字符串操作函数来处理字符串,这些函数可以帮助我们更加方便地操作字符串,...

    2 个月前

相关推荐

    暂无文章