@umbraco/headless-client
Version:
Node.js client library for the Umbraco Headless APIs
175 lines (174 loc) • 7.3 kB
TypeScript
import FormData from 'form-data';
import { Client } from '../../Client';
import { PagedResponse, ContentManagementContent, ContentManagementContentRequest } from '../../Responses';
import { APIContentChildrenOptions, APIContentPublishOptions, APIContentUnpublishOptions } from '../../RequestOptions';
/**
* ContentManagementClient is used to access the Content part of the Content Management API.
* @public
*
* @example
* The {@link ContentManagementClient} must be accessed through {@link Client}.
*
* ```typescript
* import { Client } from '@umbraco/headless-client'
*
* const client = new Client({
* projectAlias: '<your-project-alias>',
* apiKey: '<your-api-key>',
* language: '<iso-code>',
* })
*
* const contentClient = client.management.content
* ```
*/
export declare class ContentManagementClient {
private readonly client;
/**
* @internal
*/
constructor(client: Client);
private readonly makeRequest;
/**
* Fetch all content at the root of the tree, which the authorized user has access to according to the 'Start node'-permissions.
* @returns a `Promise` that resolves to an array of {@link ContentManagementContent},
*/
root<T extends ContentManagementContent>(): Promise<T[]>;
/**
* Fetch a single Content item by its id.
* @param id - GUID id of the Content item.
* @returns a `Promise` that resolves to a {@link ContentManagementContent} if found, otherwise `undefined`.
*/
byId<T extends ContentManagementContent>(id: string): Promise<T | undefined>;
/**
* Fetch all children of a Content item.
* @param id - GUID id of the Content item.
* @param options - Request options. See {@link APIContentChildrenOptions}.
* @returns a `Promise` that resolves to a {@link PagedResponse} of {@link ContentManagementContent} if found, otherwise `undefined`.
*/
children<T extends ContentManagementContent>(id: string, options?: APIContentChildrenOptions): Promise<PagedResponse<T> | undefined>;
/**
* Create a new Content item.
* @param body - The Content to create. See {@link ContentManagementContentRequest}.
* @returns a `Promise` that resolves to the newly created {@link ContentManagementContent}.
*
* @example
* ```typescript
* const content = await client.management.content.create({
* name: {
* $invariant: '<name>',
* },
* contentTypeAlias: '<content-type-alias>',
* parentId: '<parentId|undefined>',
* })
* ```
*
* If the Content Type includes an `Upload` or an `Image Cropper` property and you want to upload a file you need to pass a `FormData` object to the function instead,
*
* ```typescript
* import FormData from `form-data`
* import fs from 'fs'
* import path from 'path'
*
* const data = new FormData()
*
* data.append(JSON.stringify({
* name: {
* $invariant: '<name>',
* },
* contentTypeAlias: '<content-type-alias>',
* parentId: '<parentId|undefined>',
* // if myFile is of type `Upload` and is culture variant
* myFile: {
* 'en-US': 'my-file.txt',
* },
* // if myImage is of type `Image Cropper` and is culture invariant
* myImage: {
* $invariant: {
* src: 'my-image.jpg',
* },
* },
* }))
*
* data.append('myFile.en-US', fs.createReadStream(path.join(__dirname, 'my-file.txt')))
* data.append('myImage.$invariant', fs.createReadStream(path.join(__dirname, 'my-image.txt')))
*
* const content = await client.management.content.create(data)
* ```
*
* See {@link https://our.umbraco.com/documentation/Umbraco-Heartcore/API-Documentation/Content-Management/content/#create-content} for more info on the structure of the document.
*/
create<T extends ContentManagementContent>(body: ContentManagementContentRequest | FormData): Promise<T>;
/**
* Publish a Content item.
* @param id - GUID id of the Content item.
* @param options - Request options. See {@link APIContentPublishOptions}.
* @returns a `Promise` that resolves to a {@link ContentManagementContent} if found, otherwise `undefined`.
*/
publish<T extends ContentManagementContent>(id: string, options?: APIContentPublishOptions): Promise<T | undefined>;
/**
* Unpublish a Content item.
* @param id - GUID id of the Content item.
* @param options - Request options. See {@link APIContentPublishOptions}.
* @returns a `Promise` that resolves to a {@link ContentManagementContent} if found, otherwise `undefined`.
*/
unPublish<T extends ContentManagementContent>(id: string, options?: APIContentUnpublishOptions): Promise<T | undefined>;
/**
* Update a Content item.
* @param id - GUID id of the Content item.
* @param body - Content to update, must be a complete Content item including all cultures. See {@link ContentManagementContentRequest}.
* @returns a `Promise` that resolves to a {@link ContentManagementContent} of the updated Content item if found, otherwise `undefined`.
*
* @example
* ```typescript
* const content = await client.management.content.update('<content-id>', {
* name: {
* $invariant: '<name>',
* },
* contentTypeAlias: '<content-type-alias>',
* parentId: '<parentId|undefined>',
* })
* ```
*
* If the Content Type includes an `Upload` or an `Image Cropper` property and you want to upload a file you need to pass a `FormData` object to the function instead,
*
* ```typescript
* import FormData from `form-data`
* import fs from 'fs'
* import path from 'path'
*
* const data = new FormData()
*
* data.append(JSON.stringify({
* name: {
* $invariant: '<name>',
* },
* contentTypeAlias: '<content-type-alias>',
* parentId: '<parentId|undefined>',
* // if myFile is of type `Upload` and is culture variant
* myFile: {
* 'en-US': 'my-file.txt',
* },
* // if myImage is of type `Image Cropper` and is culture invariant
* myImage: {
* $invariant: {
* src: 'my-image.jpg',
* },
* },
* }))
*
* data.append('myFile.en-US', fs.createReadStream(path.join(__dirname, 'my-file.txt')))
* data.append('myImage.$invariant', fs.createReadStream(path.join(__dirname, 'my-image.txt')))
*
* const content = await client.management.content.update('<content-id>', data)
* ```
*
* See {@link https://our.umbraco.com/documentation/Umbraco-Heartcore/API-Documentation/Content-Management/content/#update-content} for more info on the structure of the document.
*/
update<T extends ContentManagementContent>(id: string, body: ContentManagementContentRequest | FormData): Promise<T | undefined>;
/**
* Delete a Content item.
* @param id - GUID id of the Content item.
* @returns a `Promise` that resolves to a {@link ContentManagementContent} of the deleted Content item if found, otherwise `undefined`.
*/
delete<T extends ContentManagementContent>(id: string): Promise<ContentManagementContent | undefined>;
}