Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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,
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();
});
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);
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: {
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);
};
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,
});
}
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
}
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);
};
}
ctx.serializer = (type, opts) => {
return new Serializer(type, opts);
};