### Installing Clever Cloud Client Module Source: https://github.com/clevercloud/clever-client.js/blob/master/README.md This shell command demonstrates how to install the `@clevercloud/client` Node.js module using npm. This is the first step required to use the client library in your project. ```sh npm install @clevercloud/client ``` -------------------------------- ### Streaming Application Logs with Clever Cloud Client (Javascript) Source: https://github.com/clevercloud/clever-client.js/blob/master/README.md This snippet demonstrates how to use the Clever Cloud Javascript client to establish and manage a stream of application logs. It involves importing the necessary class, configuring connection parameters with API host and OAuth tokens, instantiating the `ApplicationLogStream` with owner and app IDs, setting up event listeners for stream status and incoming log messages, and controlling the stream lifecycle (start, pause, resume). Optional configurations for retries and log filtering are also shown. ```javascript import { ApplicationLogStream } from '@clevercloud/client/esm/streams/application-log.js'; // Load and cache config and tokens const API_HOST = 'https://api.clever-cloud.com'; const tokens = { OAUTH_CONSUMER_KEY: 'your OAUTH_CONSUMER_KEY', OAUTH_CONSUMER_SECRET: 'your OAUTH_CONSUMER_SECRET', API_OAUTH_TOKEN: 'your API_OAUTH_TOKEN', API_OAUTH_TOKEN_SECRET: 'your API_OAUTH_TOKEN_SECRET', }; // Create an EventsStream instance (appId is optional) const logsStream = new ApplicationLogStream({ apiHost: API_HOST, tokens, ownerId: 'YYY', appId: 'XXX', // optionnal auto retry config // retryConfiguration: { // enabled: true, // backoffFactor: number, // initRetryTimeout: number, // maxRetryCount: number, // }, // optionnal logs request params // since: Date, // until: Date, // service: string[], // limit: number, // deploymentId: string, // instanceId: string[], // filter: string, // field: string[], // throttleElements: number, // throttlePerInMilliseconds: number, }); logsStream .on('open', (event) => console.debug('stream opened!', event)) .on('error', (event) => console.error('error', event.error)) .onLog((event) => console.log(event.data.date, event.data.message)); logsStream.start(); // You can also pause the stream logsStream.pause(); // And resume it logsStream.resume(); ``` -------------------------------- ### Wrapping API Calls with Helpers in JavaScript Source: https://github.com/clevercloud/clever-client.js/blob/master/README.md This JavaScript function shows how to create a reusable wrapper (`sendToApi`) for Clever Cloud API calls. It chains helper functions (`prefixUrl`, `addOauthHeader`, `request`) to add the API host, handle OAuth v1 authentication, and send the HTTP request using the fetch API. ```js import { addOauthHeader } from '@clevercloud/client/esm/oauth.js'; import { prefixUrl } from '@clevercloud/client/esm/prefix-url.js'; import { request } from '@clevercloud/client/esm/request.fetch.js'; export function sendToApi (requestParams) { // load and cache config and tokens const API_HOST = 'https://api.clever-cloud.com' const tokens = { OAUTH_CONSUMER_KEY: 'your OAUTH_CONSUMER_KEY', OAUTH_CONSUMER_SECRET: 'your OAUTH_CONSUMER_SECRET', API_OAUTH_TOKEN: 'your API_OAUTH_TOKEN', API_OAUTH_TOKEN_SECRET: 'your API_OAUTH_TOKEN_SECRET', } return Promise.resolve(requestParams) .then(prefixUrl(API_HOST)) .then(addOauthHeader(tokens)) .then(request); // chain a .catch() call here if you need to handle some errors or maybe redirect to login } ``` -------------------------------- ### Retrieving App Logs using LogsStream Source: https://github.com/clevercloud/clever-client.js/blob/master/README.md This JavaScript code demonstrates how to instantiate and use `LogsStream` to receive live log lines from a Clever Cloud application. It shows how to provide configuration (host, tokens, app ID) and listen for 'log' events. ```js // Browser implementation or Node.js implementation import { LogsStream } from '@clevercloud/client/esm/streams/logs.browser.js'; // import { LogsStream } from '@clevercloud/client/esm/streams/logs.node.js'; // Load and cache config and tokens const API_HOST = 'https://api.clever-cloud.com'; const tokens = { OAUTH_CONSUMER_KEY: 'your OAUTH_CONSUMER_KEY', OAUTH_CONSUMER_SECRET: 'your OAUTH_CONSUMER_SECRET', API_OAUTH_TOKEN: 'your API_OAUTH_TOKEN', API_OAUTH_TOKEN_SECRET: 'your API_OAUTH_TOKEN_SECRET', }; // Create a LogsStream instance (filter and deploymentId are optional) const logsStream = new LogsStream({ apiHost: API_HOST, tokens, appId, filter, deploymentId }); // Listen to "log" events logsStream.on('log', (rawLogLine) => console.log(rawLogLine)); // Open the stream logsStream.open(); ``` -------------------------------- ### Generating OAuth Tokens using CLI Source: https://github.com/clevercloud/clever-client.js/blob/master/README.md This shell command uses the `clever-tools` CLI to perform the login process, which also generates the necessary OAuth token and secret couple required for API authentication. ```sh clever login ``` -------------------------------- ### Retrieving Platform Events using EventsStream Source: https://github.com/clevercloud/clever-client.js/blob/master/README.md This JavaScript code demonstrates how to instantiate and use `EventsStream` to receive platform events from Clever Cloud. It requires configuration (host, tokens) and allows listening for 'event' events emitted by the stream. ```js // Browser implementation or Node.js implementation import { EventsStream } from '@clevercloud/client/esm/streams/events.browser.js'; // import { EventsStream } from '@clevercloud/client/esm/streams/events.node.js'; // Load and cache config and tokens const API_HOST = 'https://api.clever-cloud.com'; const tokens = { OAUTH_CONSUMER_KEY: 'your OAUTH_CONSUMER_KEY', OAUTH_CONSUMER_SECRET: 'your OAUTH_CONSUMER_SECRET', API_OAUTH_TOKEN: 'your API_OAUTH_TOKEN', API_OAUTH_TOKEN_SECRET: 'your API_OAUTH_TOKEN_SECRET', }; // Create an EventsStream instance (appId is optional) const eventsStream = new EventsStream({ apiHost: API_HOST, tokens, appId }); // Listen to "event" events eventsStream.on('event', (rawEvent) => console.log(rawEvent)); // Open the stream eventsStream.open(); ``` -------------------------------- ### Calling Clever Cloud API Function and Sending Request Source: https://github.com/clevercloud/clever-client.js/blob/master/README.md This JavaScript code snippet demonstrates how to use a specific API function (`getAllEnvVars`) from the client library. It calls the function with necessary parameters and then chains the custom `sendToApi` helper function to execute the request and handle authentication and transmission, using `await` for asynchronous handling. ```js import { getAllEnvVars } from '@clevercloud/client/esm/api/v2/application.js'; import { sendToApi } from '../send-to-api.js'; const envVars = await getAllEnvVars({ id: oid, appId }).then(sendToApi); ``` -------------------------------- ### Configuring Custom Auto Retry Options Source: https://github.com/clevercloud/clever-client.js/blob/master/README.md This JavaScript code demonstrates how to open a stream with the automatic retry behavior enabled and customize its parameters, including the exponential backoff factor, the initial retry timeout, and the maximum number of retry attempts. ```js stream.open({ autoRetry: true, // Factor used to compute exponential backoff delays, defaults to 1.25 backoffFactor: 1.5, // First iteration timeout in ms, also used to compute exponential backoff delays, defaults to 1500 initRetryTimeout: 2000, // Maximum number of consecutive iterations the auto retry behaviour can do, defaults to Infinity maxRetryCount: 6, }); ``` -------------------------------- ### Generating REST Client Code Source: https://github.com/clevercloud/clever-client.js/blob/master/README.md This npm script command triggers the process to generate both ECMAScript modules (ESM) and CommonJS (CJS) versions of the Clever Cloud REST client code based on the OpenAPI specification. ```sh npm run generate-client ``` -------------------------------- ### Configure Git Hooks Path for Linter (Shell) Source: https://github.com/clevercloud/clever-client.js/blob/master/CONTRIBUTING.md This shell snippet changes the current directory to the repository root and configures Git's `core.hooksPath` setting to point to the `.githooks` directory. This is typically done to enable project-specific Git hooks, such as a commit message linter that checks conventional commit compliance. ```shell cd ${PATH_TO_THE_REPOSITORY_ROOT} git config core.hooksPath '.githooks' ``` -------------------------------- ### Generating ESM Client from OpenAPI Source: https://github.com/clevercloud/clever-client.js/blob/master/README.md This npm script command specifically generates the ECMAScript modules (ESM) version of the Clever Cloud REST client code by fetching and processing the OpenAPI document. ```sh npm run generate-client-from-openapi ``` -------------------------------- ### Opening Stream with Auto Retry Enabled Source: https://github.com/clevercloud/clever-client.js/blob/master/README.md This JavaScript code demonstrates how to open a stream (`LogsStream` or `EventsStream`) and enable the automatic retry behavior. When enabled, the stream will attempt to reconnect automatically upon detecting network failures. ```js stream.open({ autoRetry: true }); ``` -------------------------------- ### Listening to Auto Retry Events Source: https://github.com/clevercloud/clever-client.js/blob/master/README.md This JavaScript code shows how to subscribe to events (`close`, `open`, `ping`) emitted by a stream when the auto retry behavior is enabled. These events provide insight into the stream's connection status and retry attempts. ```js // "close" event is emitted each time a network failure is detected (or any unknown error not related to authentication) stream.on('close', (reason) => console.log('Stream closed because of', reason)); // "open" event is emitted on first .open() call and each time a new open is attempted stream.on('open', () => console.log('Stream open...')); // "ping" event is emitted each time a ping is received from the source stream stream.on('ping', () => console.log('Received ping')); ``` -------------------------------- ### Opening LogsStream with Custom Callbacks - JavaScript Source: https://github.com/clevercloud/clever-client.js/blob/master/docs/adr-0002-why-exposing-data-as-events.md This JavaScript snippet demonstrates the old, custom callback-based API previously used in the clever-client for handling data streams like logs. It shows how to instantiate a `LogsStream` object and register `onMessage` and `onError` listeners directly when opening the stream. This approach had limitations regarding listener management and API standardization, leading to the adoption of event-based APIs. ```javascript const logsStream = new LogsStream({ apiHost, tokens, /* ... */ }); const closeStreamFn = logsStream.openResilientStream({ onMessage: (msg) => console.log(msg), onError: (error) => console.error(error), }); ``` -------------------------------- ### Handling Stream Errors Source: https://github.com/clevercloud/clever-client.js/blob/master/README.md This JavaScript code snippet shows how to attach an event listener for the 'error' event on a stream instance. This allows handling network failures, bad authentication, or other issues that cause the stream to fail. ```js stream.on('error', (error) => console.error(error)); ``` -------------------------------- ### Clearing OpenAPI Cache Source: https://github.com/clevercloud/clever-client.js/blob/master/README.md This npm script command removes the locally cached OpenAPI document used during the client generation process, ensuring that the next generation uses a fresh document. ```sh npm run clean-cache ``` -------------------------------- ### Consuming Async Iterator with for await...of in JavaScript Source: https://github.com/clevercloud/clever-client.js/blob/master/docs/adr-0002-why-exposing-data-as-events.md This snippet demonstrates the standard way to iterate over values provided by an async iterator using the `for await...of` loop. It assumes that the `readLines(file)` function returns an object implementing the async iterator protocol. This pattern is suitable for consuming sequences of data that are generated asynchronously. ```js // In this example readLines(file) returns an async iterator for await (const line of readLines(file)) { console.log(line); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.