
Product
Introducing GitHub Actions Scanning Support
Detect malware, unsafe data flows, and license issues in GitHub Actions with Socket’s new workflow scanning support.
@mediamonks/jsonapi-client
Advanced tools
TypeScript JSON:API formatter and client with type safe sparse fieldsets and compound documents
JSON:API-Client is a JSON:API formatter and client in TypeScript with type safe sparse fieldsets and compound documents.
⚠️ This is an experimental branch, its implementation is not completed, not tested, and not production ready.
as const.A JSON:API resource is defined with ResourceFormatter, when a resource has a circular reference (i.e. a resource references itself directly or in one of its descendants) you will need to predefine the formatter type to prevent typescript going berserk. Because your model might change in the future it is recommended to always define your type to prevent seemingly random errors in the future.
For example:
type UserFormatter = ResourceFormatter<
'User',
{
emailAddress: Attribute.Required<string>
password: Attribute.RequiredWriteOnly<string>
userName: Attribute.RequiredStatic<string>
dateOfBirth: Attribute.Optional<string, Date>
role: Attribute.RequiredReadOnly<'admin' | 'editor' | 'subscriber'>
messages: Attribute.ToManyReadOnly<MessageResource>
friends: Relationship.ToMany<UserResource>
}
>
// An attribute field can use a type formatter to (de)serialize its values, for example to
// automatically convert an ISO8601 (date) string to a Date object back and forth.
const dateStringFormatter = {
serialize: (value: Date) => value.toISOString(),
deserialize: (value: string) => new Date(value),
}
// Type is an included composable assertion library to check values against their desired types.
const string = Type.is('a string', isString)
const userRole = Type.either('admin', 'editor', 'subscriber')
const user: UserFormatter = new ResourceFormatter('User', {
emailAddress: Attribute.required(string),
password: Attribute.requiredWriteOnly(string),
userName: Attribute.requiredStatic(string),
dateOfBirth: Attribute.optional(string, dateStringFormatter),
role: Attribute.requiredReadOnly(userRole),
messages: Relationship.toMany(() => message),
friends: Relationship.toMany(() => user), // <- friends has a circular reference to user
})
const url = new URL('https://example.com/api/')
const client = new Client(url)
const userPath = 'users'
const userEndpoint = client.endpoint(userPath, user)
const myFirstUser = {
emailAddress: 'jane.smiht@example.com',
password: 'password1',
userName: 'jane',
}
userEndpoint.create(myFirstUser).then((user) => {
console.log(user.messages)
})
const myFirstUserUpdate = {
emailAddress: 'jane.smith@example.com',
}
userEndpoint.update('1', myFirstUserUpdate)
const userEmailFilter = user.createFilter({
fields: {
[user.type]: ['emailAddress', 'dateOfBirth'],
},
})
userEndpoint.getOne('12', userEmailFilter).then((user) => {
console.log(user)
/* Resource {
type: 'User',
id: string,
emailAddress: string,
dateOfBirth: Date | null,
}
*/
})
(ResourceType, ResourceFields) -> ResourceFormatter
ResourceFormatter factory, returns a ResourceFormatter with methods to perform operations on (formatted) JSON:API data for this resource.
(ResourceId) -> ResourceIdentifier
Create a ResourceIdentifier from the Resource static type and id parameter
(ResourceFieldsQuery, ResourceIncludeQuery) -> ResourceFilter
(JSONAPIDocument, ResourceFilter?) -> Resource
(ResourcePostData) -> JSONAPIResourceObject
(ResourceId, ResourcePatchData) -> JSONAPIResourceObject
(ResourceFieldRoot, ResourceFieldFlag) -> ResourceField
(ResourceFieldFlag) -> boolean
Extends ResourceField
([ResourceFieldRule, ResourceFieldRule, ResourceFieldRule]) -> (Predicate, AttributeFieldFormatter) -> AttributeField<T, AttributeValue, ResourceFieldFlag>
Extends ResourceField
([ResourceFieldRule, ResourceFieldRule, ResourceFieldRule]) -> (Predicate, AttributeFieldFormatter) -> AttributeField<ResourceConstructor, RelationshipFieldType.ToOne, ResourceFieldFlag>
([ResourceFieldRule, ResourceFieldRule, ResourceFieldRule]) -> (Predicate, AttributeFieldFormatter) -> AttributeField<ResourceConstructor, RelationshipFieldType.ToMany, ResourceFieldFlag>
(URL, ClientSetup) -> JSONAPIClient
async (ResourceConstructor, ResourceCreateData) -> OneResource
async (ResourceConstructor, ResourceId, ResourcePatchData) -> void
async (ResourceConstructor, ResourceId) -> void
async (ResourceConstructor, ResourceId, ToManyRelationshipNameWithFlag, RelationshipPatchData) -> void
async (ResourceConstructor, ResourceId, ToManyRelationshipNameWithFlag, ToManyRelationshipPatchData) -> void
async (ResourceConstructor, ResourceId, ToManyRelationshipNameWithFlag, ToManyRelationshipPatchData) -> void
async (ResourceConstructor, ResourceId, ResourceQuery) -> OneResource
async (ResourceConstructor, ClientQuery, ResourceQuery) -> ManyResource
async (ResourceConstructor, ResourceId, ToManyRelationshipName, ResourceQuery) -> OneResource
async (ResourceConstructor, ResourceId, ToManyRelationshipName, ResourceQuery) -> ManyResource
(ResourceConstructor, ResourceQuery) -> (ResourceId) -> OneResource
(ResourceConstructor, ResourceQuery) -> (ClientQuery) -> ManyResource
(ResourceConstructor, ToOneRelationshipFieldNameWithFlag, ResourceQuery) -> (ResourceId) -> OneResource
(ResourceConstructor, ToManyRelationshipFieldNameWithFlag, ResourceQuery) -> (ResourceId) -> ManyResource
(FilteredResource, JSONAPIMetaObject, JSONAPIResourceLinks) -> OneResource
(FilteredResource[], JSONAPIMetaObject, JSONAPIResourceLinks & JSONAPIPaginationLinks) -> ManyResource
FAQs
TypeScript JSON:API formatter and client with type safe sparse fieldsets and compound documents
The npm package @mediamonks/jsonapi-client receives a total of 129 weekly downloads. As such, @mediamonks/jsonapi-client popularity was classified as not popular.
We found that @mediamonks/jsonapi-client demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 9 open source maintainers 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.

Product
Detect malware, unsafe data flows, and license issues in GitHub Actions with Socket’s new workflow scanning support.

Product
Add real-time Socket webhook events to your workflows to automatically receive pull request scan results and security alerts in real time.

Research
The Socket Threat Research Team uncovered malicious NuGet packages typosquatting the popular Nethereum project to steal wallet keys.