Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
@janiscommerce/api-save
Advanced tools
A package to handle Janis Save APIs
npm install @janiscommerce/api-save
'use strict';
const { ApiSaveData } = require('@janiscommerce/api-save');
const { struct } = require('@janiscommerce/superstruct');
const MyRelatedModel = require('../../models/my-related-model');
const someAsyncTask = require('./async-task');
module.exports = class MyApiSaveData extends ApiSaveData {
static get relationshipsParameters() {
return {
children: {
modelClass: MyRelatedModel,
mainIdentifierField: 'dbFieldForMainEntity',
secondaryIdentifierField: 'dbFieldForRelatedEntity',
shouldClean: false
}
};
}
static get idStruct() {
return struct.optional('string|number');
}
static get mainStruct() {
return struct.partial({
name: 'string',
description: 'string?',
status: 'number'
});
}
static get relationshipsStruct() {
return struct.partial({
children: ['string']
});
}
postStructValidate() {
return someAsyncTask(this.dataToSave.main);
}
format({ someField, ...restOfTheRecord }) {
return {
...restOfTheRecord,
someField: `prefix-${someField}`
};
}
async shouldSave(formattedItem) {
const currentItem = await this.getCurrent();
return formattedItem.someField !== currentItem.someField;
}
postSaveHook(id, savedData) {
return someAsyncTask(id, savedData);
}
};
The following getters and methods can be used to customize and validate your Save API.
All of them are optional, however you're encouraged to implement static get mainStruct()
so you don't save trash data in your DB.
You need to use this in case you're saving relationships with other models (mostly for relational databases) If you don't have any relationship, there's no need to implement it.
This getter must return an object mapping the name of the field that contains the relationship (must be a key in the struct's relationships
property) to the parameters of that relationship.
The parameters contain the following properties:
false
This is used to validate the ID received as path parameter. Defaults to an optional string or number.
This is used to validate the data received in the request, checking the data to be saved in the main entity. Defaults to an object with any property.
This is used to validate the data received in the request, checking the data to be passed to the relationships. Defaults to an object partial with no properties.
This is used to validate the data received in the request, making additional validation even injecting data to the received data. If it returns a Promise, it will be awaited.
You can use this to format your main record before it's saved. For example, mapping user friendly values to DB friendly values, add default values, etc. If it returns a Promise, it will be awaited.
This an optional method allows you to validate if saving the item is really necessary.
This method is called after formatting the item with format()
.
insert
the new item or update
the current. The API will response the status code 200 adding the id
if received at the response body.recordId
) the API will set the status code 204 No Content
.You can use this to obtain the current item for DB. It only works when the API receives the id
in the Endpoint (API PUT or PATCH)
This method will throw an Error if is used in an API POST (without recordId
)
You can use this to perform a task after saving your main record. For example, emitting an event, logging something, etc. If it returns a Promise, it will be awaited.
FAQs
A package to handle Janis Save APIs
The npm package @janiscommerce/api-save receives a total of 281 weekly downloads. As such, @janiscommerce/api-save popularity was classified as not popular.
We found that @janiscommerce/api-save demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.