How to use jsonapi-serializer - 10 common examples

To help you get started, we’ve selected a few jsonapi-serializer examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github shoutem / cli / src / clients / json-api-client.js View on Github external
import _ from 'lodash';
import { Deserializer } from 'jsonapi-serializer';
import * as logger from '../services/logger';

const deserializer = new Deserializer({
  keyForAttribute: 'camelCase'
});

// jsonapi-serializer seems to ignore non-included relationships no matter what its docs say
// therefore, support an option to use our own deserialization when specified(default still jsonapi-serializer)
function deserializePlainSingle(json) {
  const relationships = json.relationships;
  const relationshipKeys = _.keys(relationships);

  const relationshipIds = {};
  _.forEach(relationshipKeys, key => {
    relationshipIds[key] = _.get(relationships[key], 'data.id');
  })

  return {
    ...relationshipIds,
github oberonamsterdam / jsonapi-mock / src / index.js View on Github external
import bodyParser from 'body-parser';
import cookieParser from 'cookie-parser';
import express from 'express';
import http from 'http';
import jsonapiSerializer from 'jsonapi-serializer';
import logger from 'morgan';
import { globalAccept, globalContentType, mainRoutes, port } from './constants/Globals';
import router from './routes/routes';
import './services/CheckEnvVars';
import { isValid, NotFoundhandler, onError } from './services/Helpers';

// const declaration
const app = express();
const JSONAPIError = jsonapiSerializer.Error;

// middleware
app.use(logger('dev'));
app.use(bodyParser.json({ type: globalContentType }));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());

app.use((req, res, next) => {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
    res.header('Content-Type', globalContentType);
    res.header('Accept', globalContentType);
    res.removeHeader('X-Powered-By');
    next();
});
github DefinitelyTyped / DefinitelyTyped / types / jsonapi-serializer / jsonapi-serializer-tests.ts View on Github external
import { Serializer, Deserializer, Error } from "jsonapi-serializer";

declare let firstName: string;
declare let lastName: string;

const UserSerializer = new Serializer("users", {
    id: "id",
    attributes: ["firstName", "lastName"],
    keyForAttribute: "camelCase",
    pluralizeType: false
});

const users = UserSerializer.serialize({ firstName, lastName });

const UserDeserializer = new Deserializer({
    id: "id",
    keyForAttribute: "camelCase",
    pluralizeType: false,
    typeAsAttribute: true
});

UserDeserializer.deserialize(users);
github DefinitelyTyped / DefinitelyTyped / types / jsonapi-serializer / jsonapi-serializer-tests.ts View on Github external
import { Serializer, Deserializer, Error } from "jsonapi-serializer";

declare let firstName: string;
declare let lastName: string;

const UserSerializer = new Serializer("users", {
    id: "id",
    attributes: ["firstName", "lastName"],
    keyForAttribute: "camelCase",
    pluralizeType: false
});

const users = UserSerializer.serialize({ firstName, lastName });

const UserDeserializer = new Deserializer({
    id: "id",
    keyForAttribute: "camelCase",
    pluralizeType: false,
    typeAsAttribute: true
});

UserDeserializer.deserialize(users);

new Error({
    code: "_code",
    detail: "_detail",
    id: "_id",
    links: {
        about: "_about"
    },
    source: {
github mansona / express-autoroute-json / lib / serialise.js View on Github external
if (Array.isArray(data)) {
        data.forEach(item => _.assign(item, { [key]: '****' }));
      } else {
        _.assign(data, { [key]: '****' });
      }

      schema[key] = {
        ref: function(object) {
          return object._id;
        },
      };
    });
  }

  var serializer = new JSONAPISerializer(type, schema);

  return serializer.serialize(data);
};
github ForestAdmin / forest-express / src / integrations / intercom / serializers / intercom-attributes.js View on Github external
function IntercomAttributesSerializer(attributes, collectionName, meta) {
  const type = `${collectionName}_intercom_attributes`;

  return new JSONAPISerializer(type, attributes, {
    attributes: ['session_count', 'last_seen_ip', 'created_at', 'updated_at',
      'signed_up_at', 'last_request_at', 'country', 'city', 'user_agent',
      'companies', 'segments', 'tags', 'browser', 'platform', 'geoloc'],
    keyForAttribute(key) { return key; },
    meta,
  });
}
github ForestAdmin / forest-express-sequelize / serializers / stripe-invoices.js View on Github external
if (invoice.period_start) {
      invoice.period_start =  new Date(invoice.period_start * 1000);
    }

    if (invoice.period_end) {
      invoice.period_end =  new Date(invoice.period_end * 1000);
    }

    if (invoice.subtotal) { invoice.subtotal /= 100; }
    if (invoice.total) { invoice.total /= 100; }

    return invoice;
  });

  return new JSONAPISerializer('stripe-invoices', invoices, {
    attributes: ['amount_due', 'attempt_count', 'attempted', 'closed',
      'currency', 'date', 'forgiven', 'paid', 'period_start', 'period_end',
      'subtotal', 'total', 'application_fee', 'tax', 'tax_percent',
      'customer'],
    customer: {
      ref: 'id',
      attributes: customerAttributes
    },
    keyForAttribute: function (key) {
      return Inflector.underscore(key);
    },
    typeForAttribute: function (attr) {
      if (attr === 'customer') { return customerCollectionName; }
      return attr;
    },
    meta: meta
github ForestAdmin / forest-express-sequelize / serializers / resource.js View on Github external
}

    var serializationOptions = {
      attributes: _.map(schema.fields, 'field'),
      keyForAttribute: function (key) {
        return key;
      },
      typeForAttribute: function (attribute) {
        return typeForAttributes[attribute] || attribute;
      },
      meta: meta
    };

    getAttributesFor(serializationOptions, schema.fields);

    return new JSONAPISerializer(schema.name, records,
      serializationOptions);
  };
}
github villeristi / koa-api-boilerplate / src / config / middleware.js View on Github external
ctx.serializer = (type, opts) => {
    return new Serializer(type, opts);
  };

jsonapi-serializer

A Node.js framework agnostic library for serializing your data to JSON API

MIT
Latest version published 8 months ago

Package Health Score

67 / 100
Full package analysis