Skip to content

Latest commit

 

History

History
370 lines (267 loc) · 26.8 KB

File metadata and controls

370 lines (267 loc) · 26.8 KB

Oauth2

Overview

Available Operations

authorize

Authorize

Example Usage

import { Polar } from "@polar-sh/sdk";

const polar = new Polar({
  accessToken: process.env["POLAR_ACCESS_TOKEN"] ?? "",
});

async function run() {
  const result = await polar.oauth2.authorize();

  console.log(result);
}

run();

Standalone function

The standalone function version of this method:

import { PolarCore } from "@polar-sh/sdk/core.js";
import { oauth2Authorize } from "@polar-sh/sdk/funcs/oauth2Authorize.js";

// Use `PolarCore` for best tree-shaking performance.
// You can create one instance of it to use across an application.
const polar = new PolarCore({
  accessToken: process.env["POLAR_ACCESS_TOKEN"] ?? "",
});

async function run() {
  const res = await oauth2Authorize(polar);
  if (res.ok) {
    const { value: result } = res;
    console.log(result);
  } else {
    console.log("oauth2Authorize failed:", res.error);
  }
}

run();

Parameters

Parameter Type Required Description
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.Oauth2AuthorizeResponseOauth2Authorize>

Errors

Error Type Status Code Content Type
errors.SDKError 4XX, 5XX */*

token

Request an access token using a valid grant.

Example Usage

import { Polar } from "@polar-sh/sdk";

const polar = new Polar();

async function run() {
  const result = await polar.oauth2.token({
    grantType: "authorization_code",
    clientId: "<id>",
    clientSecret: "<value>",
    code: "<value>",
    redirectUri: "https://memorable-season.name",
  });

  console.log(result);
}

run();

Standalone function

The standalone function version of this method:

import { PolarCore } from "@polar-sh/sdk/core.js";
import { oauth2Token } from "@polar-sh/sdk/funcs/oauth2Token.js";

// Use `PolarCore` for best tree-shaking performance.
// You can create one instance of it to use across an application.
const polar = new PolarCore();

async function run() {
  const res = await oauth2Token(polar, {
    grantType: "authorization_code",
    clientId: "<id>",
    clientSecret: "<value>",
    code: "<value>",
    redirectUri: "https://memorable-season.name",
  });
  if (res.ok) {
    const { value: result } = res;
    console.log(result);
  } else {
    console.log("oauth2Token failed:", res.error);
  }
}

run();

Parameters

Parameter Type Required Description
request operations.Oauth2RequestTokenRequestBody ✔️ 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<components.TokenResponse>

Errors

Error Type Status Code Content Type
errors.SDKError 4XX, 5XX */*

revoke

Revoke an access token or a refresh token.

Example Usage

import { Polar } from "@polar-sh/sdk";

const polar = new Polar();

async function run() {
  const result = await polar.oauth2.revoke({
    token: "<value>",
    clientId: "<id>",
    clientSecret: "<value>",
  });

  console.log(result);
}

run();

Standalone function

The standalone function version of this method:

import { PolarCore } from "@polar-sh/sdk/core.js";
import { oauth2Revoke } from "@polar-sh/sdk/funcs/oauth2Revoke.js";

// Use `PolarCore` for best tree-shaking performance.
// You can create one instance of it to use across an application.
const polar = new PolarCore();

async function run() {
  const res = await oauth2Revoke(polar, {
    token: "<value>",
    clientId: "<id>",
    clientSecret: "<value>",
  });
  if (res.ok) {
    const { value: result } = res;
    console.log(result);
  } else {
    console.log("oauth2Revoke failed:", res.error);
  }
}

run();

Parameters

Parameter Type Required Description
request components.RevokeTokenRequest ✔️ 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<components.RevokeTokenResponse>

Errors

Error Type Status Code Content Type
errors.SDKError 4XX, 5XX */*

introspect

Get information about an access token.

Example Usage

import { Polar } from "@polar-sh/sdk";

const polar = new Polar();

async function run() {
  const result = await polar.oauth2.introspect({
    token: "<value>",
    clientId: "<id>",
    clientSecret: "<value>",
  });

  console.log(result);
}

run();

Standalone function

The standalone function version of this method:

import { PolarCore } from "@polar-sh/sdk/core.js";
import { oauth2Introspect } from "@polar-sh/sdk/funcs/oauth2Introspect.js";

// Use `PolarCore` for best tree-shaking performance.
// You can create one instance of it to use across an application.
const polar = new PolarCore();

async function run() {
  const res = await oauth2Introspect(polar, {
    token: "<value>",
    clientId: "<id>",
    clientSecret: "<value>",
  });
  if (res.ok) {
    const { value: result } = res;
    console.log(result);
  } else {
    console.log("oauth2Introspect failed:", res.error);
  }
}

run();

Parameters

Parameter Type Required Description
request components.IntrospectTokenRequest ✔️ 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<components.IntrospectTokenResponse>

Errors

Error Type Status Code Content Type
errors.SDKError 4XX, 5XX */*

userinfo

Get information about the authenticated user.

Example Usage

import { Polar } from "@polar-sh/sdk";

const polar = new Polar({
  accessToken: process.env["POLAR_ACCESS_TOKEN"] ?? "",
});

async function run() {
  const result = await polar.oauth2.userinfo();

  console.log(result);
}

run();

Standalone function

The standalone function version of this method:

import { PolarCore } from "@polar-sh/sdk/core.js";
import { oauth2Userinfo } from "@polar-sh/sdk/funcs/oauth2Userinfo.js";

// Use `PolarCore` for best tree-shaking performance.
// You can create one instance of it to use across an application.
const polar = new PolarCore({
  accessToken: process.env["POLAR_ACCESS_TOKEN"] ?? "",
});

async function run() {
  const res = await oauth2Userinfo(polar);
  if (res.ok) {
    const { value: result } = res;
    console.log(result);
  } else {
    console.log("oauth2Userinfo failed:", res.error);
  }
}

run();

Parameters

Parameter Type Required Description
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.Oauth2UserinfoResponseOauth2Userinfo>

Errors

Error Type Status Code Content Type
errors.SDKError 4XX, 5XX */*