1.0.5 • Published 5 months ago

@content-sync/content-cloud-cli v1.0.5

Weekly downloads
-
License
MIT
Repository
-
Last release
5 months ago

Content Cloud CLI

You can use the Content Cloud CLI mainly to auto-generate client code for your project.

Installation

npm install -g @content-sync/content-cloud-cli

Settings

Base URL

The URL of the publisher to connect to. This will depend on the region of your space and can be viewed in the space settings of the Content Sync app. You can use the CC_BASE_URL environment variable to set this or pass a -b / --base-url parameter to your command.

Access token

The access token to use for all requests. This token must have access to the service:publisher API. Permissions depend on the command you want to run. You can use the CC_ACCESS_TOKEN environment variable to set this or pass a -a / --access-token parameter to your command.

The token can include an environment ID that may alter the result of the command. Double check that you are using the correct environment ID e.g. when generating code to receive the correct information.

Commands

Help

Print information about how to use the CLI or a specific command:

# General information
content-cloud help

# Help with the command `generate`
content-cloud help generate

Info

Print basic information about the connected space and token:

# General information about the space and token
content-cloud info

Generate

Below examples save the files at ./app/content-cloud but you can change this to match your project folder structure.

Typescript REST client

content-cloud generate rest typescript -o app/content-cloud

E.g. in your Next.js app, you can use the generated client like this:

import { ContentCloudRestClient } from "@/app/content-cloud/content-cloud-rest-client";

export default async function ContentList() {
  // For public APIs or when providing the access token statically as an environment variable:
  const contentCloud = new ContentCloudRestClient();

  const contentList = await contentCloud.contentCollection({
    // E.g. to filter by content type (good for auto completion and type safety):
    //content_type: "BasicPageContent",
  });

  return (
    <div>
      <h1>Content List</h1>
      {contentList.items.map((content) => (
        <div key={content.sys.id}>
          <h2>{content.sys.name}</h2>
          {content.sys.slug && <p>Maybe add a route for: {content.sys.slug}</p>}
        </div>
      ))}
    </div>
  );
}

With authentication

Never share client secrets with your users or serve it to browsers; only expose the JWTs you are creating to your end users using the client secret.

To generate a JWT and use it to request content, adjust your code like this:

import { ContentCloudPermission, ContentCloudService, generateAccessToken } from "@/app/content-cloud/content-cloud-authentication";
// ...

export default async function ContentList() {
  // For private APIs without a static access token:
  const contentCloud = new ContentCloudRestClient({
    accessToken: generateAccessToken({
        permissions: [
          // The base permission to query content.
          ContentCloudPermission.CONTENT_READ,
          // To download assets and display images.
          ContentCloudPermission.ASSET_READ_FILE,
          // To read and write user data.
          ContentCloudPermission.USER_DATA_READ,
          ContentCloudPermission.USER_DATA_WRITE,
        ],
        // The APIs the user is allowed to access.
        services: [ContentCloudService.LIVE, ContentCloudService.ASSETS],
        // From your auth system, e.g. Auth0.
        userId: 'test-user-id',
        userDataContentTypes: ['*'],
      }),
  });

  //...
}

Typescript GraphQL client

content-cloud generate graphql typescript -o app/content-cloud

GraphQL schema

content-cloud graphql graphql typescript -o app/content-cloud
1.0.5

5 months ago

1.0.4

5 months ago

1.0.3

5 months ago

1.0.2

5 months ago

1.0.1

5 months ago

1.0.0

5 months ago