Skip to content

Files

461 lines (341 loc) · 27.6 KB

File metadata and controls

461 lines (341 loc) · 27.6 KB

CodeSamples

(codeSamples)

Overview

REST APIs for retrieving Code Samples

Available Operations

generateCodeSamplePreview

This endpoint generates Code Sample previews from a file and configuration parameters.

Example Usage

import { Speakeasy } from "@speakeasy-api/speakeasy-client-sdk-typescript";
import { openAsBlob } from "node:fs";

const speakeasy = new Speakeasy({
  security: {
    apiKey: "<YOUR_API_KEY_HERE>",
  },
});

async function run() {
  const result = await speakeasy.codeSamples.generateCodeSamplePreview({
    language: "<value>",
    schemaFile: await openAsBlob("example.file"),
  });

  // Handle the result
  console.log(result);
}

run();

Standalone function

The standalone function version of this method:

import { SpeakeasyCore } from "@speakeasy-api/speakeasy-client-sdk-typescript/core.js";
import { codeSamplesGenerateCodeSamplePreview } from "@speakeasy-api/speakeasy-client-sdk-typescript/funcs/codeSamplesGenerateCodeSamplePreview.js";
import { openAsBlob } from "node:fs";

// Use `SpeakeasyCore` for best tree-shaking performance.
// You can create one instance of it to use across an application.
const speakeasy = new SpeakeasyCore({
  security: {
    apiKey: "<YOUR_API_KEY_HERE>",
  },
});

async function run() {
  const res = await codeSamplesGenerateCodeSamplePreview(speakeasy, {
    language: "<value>",
    schemaFile: await openAsBlob("example.file"),
  });

  if (!res.ok) {
    throw res.error;
  }

  const { value: result } = res;

  // Handle the result
  console.log(result);
}

run();

React hooks and utilities

This method can be used in React components through the following hooks and associated utilities.

Check out this guide for information about each of the utilities below and how to get started using React hooks.

import {
  // Mutation hook for triggering the API call.
  useCodeSamplesGenerateCodeSamplePreviewMutation
} from "@speakeasy-api/speakeasy-client-sdk-typescript/react-query/codeSamplesGenerateCodeSamplePreview.js";

Parameters

Parameter Type Required Description
request shared.CodeSampleSchemaInput ✔️ The request object to use for the request.
options RequestOptions Used to set various options for making HTTP requests.
options.fetchOptions RequestInit Options that are passed to the underlying HTTP request. This can be used to inject extra headers for examples. All Request options, except method and body, are allowed.
options.retries RetryConfig Enables retrying HTTP requests under certain failure conditions.

Response

Promise<shared.UsageSnippets>

Errors

Error Type Status Code Content Type
errors.ErrorT 4XX application/json
errors.ErrorT 5XX application/json

generateCodeSamplePreviewAsync

This endpoint generates Code Sample previews from a file and configuration parameters, receiving an async JobID response for polling.

Example Usage

import { Speakeasy } from "@speakeasy-api/speakeasy-client-sdk-typescript";
import { openAsBlob } from "node:fs";

const speakeasy = new Speakeasy({
  security: {
    apiKey: "<YOUR_API_KEY_HERE>",
  },
});

async function run() {
  const result = await speakeasy.codeSamples.generateCodeSamplePreviewAsync({
    language: "<value>",
    schemaFile: await openAsBlob("example.file"),
  });

  // Handle the result
  console.log(result);
}

run();

Standalone function

The standalone function version of this method:

import { SpeakeasyCore } from "@speakeasy-api/speakeasy-client-sdk-typescript/core.js";
import { codeSamplesGenerateCodeSamplePreviewAsync } from "@speakeasy-api/speakeasy-client-sdk-typescript/funcs/codeSamplesGenerateCodeSamplePreviewAsync.js";
import { openAsBlob } from "node:fs";

// Use `SpeakeasyCore` for best tree-shaking performance.
// You can create one instance of it to use across an application.
const speakeasy = new SpeakeasyCore({
  security: {
    apiKey: "<YOUR_API_KEY_HERE>",
  },
});

async function run() {
  const res = await codeSamplesGenerateCodeSamplePreviewAsync(speakeasy, {
    language: "<value>",
    schemaFile: await openAsBlob("example.file"),
  });

  if (!res.ok) {
    throw res.error;
  }

  const { value: result } = res;

  // Handle the result
  console.log(result);
}

run();

React hooks and utilities

This method can be used in React components through the following hooks and associated utilities.

Check out this guide for information about each of the utilities below and how to get started using React hooks.

import {
  // Mutation hook for triggering the API call.
  useCodeSamplesGenerateCodeSamplePreviewAsyncMutation
} from "@speakeasy-api/speakeasy-client-sdk-typescript/react-query/codeSamplesGenerateCodeSamplePreviewAsync.js";

Parameters

Parameter Type Required Description
request shared.CodeSampleSchemaInput ✔️ The request object to use for the request.
options RequestOptions Used to set various options for making HTTP requests.
options.fetchOptions RequestInit Options that are passed to the underlying HTTP request. This can be used to inject extra headers for examples. All Request options, except method and body, are allowed.
options.retries RetryConfig Enables retrying HTTP requests under certain failure conditions.

Response

Promise<operations.GenerateCodeSamplePreviewAsyncResponseBody>

Errors

Error Type Status Code Content Type
errors.ErrorT 4XX application/json
errors.ErrorT 5XX application/json

get

Retrieve usage snippets from an OpenAPI document stored in the registry. Supports filtering by language and operation ID.

Example Usage

import { Speakeasy } from "@speakeasy-api/speakeasy-client-sdk-typescript";

const speakeasy = new Speakeasy({
  security: {
    apiKey: "<YOUR_API_KEY_HERE>",
  },
});

async function run() {
  const result = await speakeasy.codeSamples.get({
    registryUrl: "https://spec.speakeasy.com/my-org/my-workspace/my-source",
    operationIds: [
      "getPets",
    ],
    methodPaths: [
      {
        method: "get",
        path: "/pets",
      },
    ],
    languages: [
      "python",
      "javascript",
    ],
  });

  // Handle the result
  console.log(result);
}

run();

Standalone function

The standalone function version of this method:

import { SpeakeasyCore } from "@speakeasy-api/speakeasy-client-sdk-typescript/core.js";
import { codeSamplesGet } from "@speakeasy-api/speakeasy-client-sdk-typescript/funcs/codeSamplesGet.js";

// Use `SpeakeasyCore` for best tree-shaking performance.
// You can create one instance of it to use across an application.
const speakeasy = new SpeakeasyCore({
  security: {
    apiKey: "<YOUR_API_KEY_HERE>",
  },
});

async function run() {
  const res = await codeSamplesGet(speakeasy, {
    registryUrl: "https://spec.speakeasy.com/my-org/my-workspace/my-source",
    operationIds: [
      "getPets",
    ],
    methodPaths: [
      {
        method: "get",
        path: "/pets",
      },
    ],
    languages: [
      "python",
      "javascript",
    ],
  });

  if (!res.ok) {
    throw res.error;
  }

  const { value: result } = res;

  // Handle the result
  console.log(result);
}

run();

React hooks and utilities

This method can be used in React components through the following hooks and associated utilities.

Check out this guide for information about each of the utilities below and how to get started using React hooks.

import {
  // Query hooks for fetching data.
  useCodeSamplesGet,
  useCodeSamplesGetSuspense,

  // Utility for prefetching data during server-side rendering and in React
  // Server Components that will be immediately available to client components
  // using the hooks.
  prefetchCodeSamplesGet,
  
  // Utilities to invalidate the query cache for this query in response to
  // mutations and other user actions.
  invalidateCodeSamplesGet,
  invalidateAllCodeSamplesGet,
} from "@speakeasy-api/speakeasy-client-sdk-typescript/react-query/codeSamplesGet.js";

Parameters

Parameter Type Required Description
request operations.GetCodeSamplesRequest ✔️ The request object to use for the request.
options RequestOptions Used to set various options for making HTTP requests.
options.fetchOptions RequestInit Options that are passed to the underlying HTTP request. This can be used to inject extra headers for examples. All Request options, except method and body, are allowed.
options.retries RetryConfig Enables retrying HTTP requests under certain failure conditions.

Response

Promise<shared.UsageSnippets>

Errors

Error Type Status Code Content Type
errors.ErrorT 4XX application/json
errors.SDKError 5XX */*

getCodeSamplePreviewAsync

Poll for the result of an asynchronous Code Sample preview generation.

Example Usage

import { Speakeasy } from "@speakeasy-api/speakeasy-client-sdk-typescript";

const speakeasy = new Speakeasy({
  security: {
    apiKey: "<YOUR_API_KEY_HERE>",
  },
});

async function run() {
  const result = await speakeasy.codeSamples.getCodeSamplePreviewAsync({
    jobID: "<id>",
  });

  // Handle the result
  console.log(result);
}

run();

Standalone function

The standalone function version of this method:

import { SpeakeasyCore } from "@speakeasy-api/speakeasy-client-sdk-typescript/core.js";
import { codeSamplesGetCodeSamplePreviewAsync } from "@speakeasy-api/speakeasy-client-sdk-typescript/funcs/codeSamplesGetCodeSamplePreviewAsync.js";

// Use `SpeakeasyCore` for best tree-shaking performance.
// You can create one instance of it to use across an application.
const speakeasy = new SpeakeasyCore({
  security: {
    apiKey: "<YOUR_API_KEY_HERE>",
  },
});

async function run() {
  const res = await codeSamplesGetCodeSamplePreviewAsync(speakeasy, {
    jobID: "<id>",
  });

  if (!res.ok) {
    throw res.error;
  }

  const { value: result } = res;

  // Handle the result
  console.log(result);
}

run();

React hooks and utilities

This method can be used in React components through the following hooks and associated utilities.

Check out this guide for information about each of the utilities below and how to get started using React hooks.

import {
  // Query hooks for fetching data.
  useCodeSamplesGetCodeSamplePreviewAsync,
  useCodeSamplesGetCodeSamplePreviewAsyncSuspense,

  // Utility for prefetching data during server-side rendering and in React
  // Server Components that will be immediately available to client components
  // using the hooks.
  prefetchCodeSamplesGetCodeSamplePreviewAsync,
  
  // Utilities to invalidate the query cache for this query in response to
  // mutations and other user actions.
  invalidateCodeSamplesGetCodeSamplePreviewAsync,
  invalidateAllCodeSamplesGetCodeSamplePreviewAsync,
} from "@speakeasy-api/speakeasy-client-sdk-typescript/react-query/codeSamplesGetCodeSamplePreviewAsync.js";

Parameters

Parameter Type Required Description
request operations.GetCodeSamplePreviewAsyncRequest ✔️ The request object to use for the request.
options RequestOptions Used to set various options for making HTTP requests.
options.fetchOptions RequestInit Options that are passed to the underlying HTTP request. This can be used to inject extra headers for examples. All Request options, except method and body, are allowed.
options.retries RetryConfig Enables retrying HTTP requests under certain failure conditions.

Response

Promise<operations.GetCodeSamplePreviewAsyncResponse>

Errors

Error Type Status Code Content Type
errors.ErrorT 4XX application/json
errors.ErrorT 5XX application/json