createFileResource

Create a file resource for a parent entity.


const resource = await drupal.createFileResource<T = DrupalFile>(
type,
body,
options?: {
params,
withAuth,
deserialize,
}
): Promise<T>
  • type: string
    • Required
    • The resource type. In most cases this is file--file.
  • body: JsonApiCreateFileResourceBody
    • Required
    • The body payload with data.
      • type: string: The resource type of the host entity. Example: media--image.
      • field: string: The name of the file field on the host entity: Example: field_media_image.
      • filename: string: The name of the file with extension: Example: avatar.jpg.
      • file: Buffer: The file.
  • options
    • Optional
    • params: JsonApiParams: JSON:API params such as filter, fields, include or sort.
    • withAuth: boolean | NextDrupalAuth:
      • Set the authentication method to use. See the authentication docs.
      • Set to true to use the authentication method configured on the client.
    • deserialize: boolean: Set to false to return the raw JSON:API response.

Examples

To create a file resource, you use the name of the file field on the parent entity.

Example: A media--image with a file field called field_media_image.

const file = await drupal.createFileResource("file--file", {
data: {
attributes: {
type: "media--image", // <-- The type of the parent resource.
field: "field_media_image", // <-- The name of the field on the parent resource.
filename: "filename.jpg",
file: await fs.readFile("/path/to/file.jpg"),
},
},
})

This will create a file--file resource.

You can then use this to create a new media--image with a relationship to the file--file.

const media = await drupal.createResource<DrupalMedia>("media--image", {
data: {
attributes: {
name: "Name for the media",
},
relationships: {
field_media_image: {
data: {
type: "file--file",
id: file.id,
},
},
},
},
})