
Security News
Vite+ Joins the Push to Consolidate JavaScript Tooling
Evan You announces Vite+, a commercial, Rust-powered toolchain built on the Vite ecosystem to unify JavaScript development and fund open source.
@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
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.
Security News
Evan You announces Vite+, a commercial, Rust-powered toolchain built on the Vite ecosystem to unify JavaScript development and fund open source.
Security News
Ruby Central’s incident report on the RubyGems.org access dispute sparks backlash from former maintainers and renewed debate over project governance.
Research
/Security News
Socket researchers uncover how threat actors weaponize Discord across the npm, PyPI, and RubyGems ecosystems to exfiltrate sensitive data.