npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@looker/sdk

v24.20.0

Published

Looker SDK

Downloads

121,661

Readme

Looker SDK

The Looker SDK for TypeScript/JavaScript works with Node and browser run-times. The SDK provides a convenient way to communicate with a Looker server's APIs.

This package supports using the Looker SDK in the browser. The @looker/sdk-node package depends on this package (@looker/sdk) and @looker/sdk-rtl.

The SDK uses a plug-in architecture for initializing and supports run-time specific transports (like NodeTransport and BrowserTransport) and different approaches for managing API authentication (like NodeSession, BrowserSession, ProxySession, and CorsSession).

Please report any issues encountered, and indicate the SDK language in the report.

Getting started

The Looker Browser SDK can be used in a browser application in 3 steps:

  • install
  • authenticate
  • use

Install the Looker SDK into your application

Using yarn:

yarn add @looker/sdk @looker/sdk-rtl

Using npm:

npm install @looker/sdk @looker/sdk-rtl

Some other dependencies may be required for your project to build and run correctly.

TypeScript SDK packages

The Looker TypeScript SDK has different packages to prevent node dependencies being linked into browser usage of the SDK (the node dependencies are not available in the browser and can cause compilation errors). There are three packages for the Typescript SDK available on npm:

  1. @looker/sdk-rtl - contains a run time library needed to invoke the Looker API methods. Referencing the @looker/sdk as a dependency should automatically pull this package in.
  2. @looker/sdk - contains the Looker API methods.
  3. @looker/sdk-node - contains the dependencies needed to run the Looker SDK in a node environment. Do NOT include this package if you are using the Looker SDK in a browser. This SDK is for use with node or ts-node.

Authenticate your API calls

All requests to the Looker API server require an access token. For browser implementations, authentication is typically achieved via OAuth as described in cors.md or a Proxy Server.

Use the SDK in your browser application

Authenticating for the browser takes more setup than authenticating for use with a Node application.

The stand-alone version of the Looker API Explorer uses OAuth and the BrowserSDK to get an authentication token for Looker API requests.

  • RunItSDK shows how to override readConfig() to get SDK configuration values.

  • RunItSDK tests support debugging the flow of RunItSDK.

  • The OAuthScene React component receives the OAuth response from the Looker server and logs the user in to retrieve the API authentication token.

Looker's OAuth support makes it possible to build a Looker SDK application that only requires the browser. If a browser application can use a proxy server instead, or already uses an existing backend server, it may be simpler to use a proxy for authentication/

The looker.ini configuration file and environment variables are never used in the Browser runtime.

Developing with multiple API versions

Only API 4.0 is currently available. API 3.1 has been removed in Looker v23.18+.

LookerBrowserSDK.init40() and Looker40SDK() initialize the API 4.0 implementation of the SDK.

Customizing HTTP request handling

With the complete removal of the deprecated request package the HTTP transport layer has been refactored to have feature parity between Node and the Browser SDK.

The following properties can be specified as part of the options parameter, which is the last parameter of every SDK method:

  • timeout to set a per-request timeout (in seconds)
  • signal to cancel cancelling requests by passing an AbortSignal or AbortController signal to an SDK request
  • automatically retryable HTTP requests
    • enable with maxTries > 1
    • optionally define a Waitable callback for custom retry wait period handling

Streaming with the SDK

The deprecated NodeJS request package dependency has been removed from all Looker TypeScript packages. This removal prompted a BREAKING interface change for the streaming SDK. The streaming method callback signature changed from (readable: Readable) => Promise<x> to (response: Response) => Promise<x>. Using Response as the parameter to the callback greatly increases the flexibility of streaming implementations and provides other valuable information like Content-Type and other headers to the streaming callback.

For the Browser SDK (@looker/sdk), the standard fetch function is still used. For the Node SDK (@looker/sdk-node), the global fetch function from NodeJS is used, which was marked stable in version 22.

This means the Looker Node SDK now requires Node 20 or above.

The streaming version of the SDK methods should be initialized using the same AuthSession as the main SDK to reduce authentication thrashing.

Construction of the streaming SDK can use code similar to the following, which is taken from the downloadTile.ts example:

/**
 * Use the streaming SDK to download a tile's query
 * @param sdk to use
 * @param tile to download
 * @param format to download
 * @returns name of downloaded file (undefined on failure)
 */
const downloadTileAs = async (
  sdk: LookerSDK,
  tile: IDashboardElement,
  format: string
) => {
  const fileName = `${tile.title}.${format}`;

  const writer = createWritableStream(fs.createWriteStream(fileName));
  const request: IRequestRunQuery = {
    result_format: format,
    query_id: tile.query_id!,
    // apply_formatting: true,
    // apply_vis: true
  };
  const sdkStream = new Looker40SDKStream(sdk.authSession);
  await sdkStream.run_query(async (response: Response) => {
    await response.body.pipeTo(writer);
    return 'streamed';
  }, request);

  return fileName;
};

Using a Proxy for authentication

CORS support allows the Looker API to be used directly in the browser application running on a different domain than the Looker server. Because all API endpoints require authentication except for Login, a proxy server can be used to retrieve the API authentication token and provide it to the browser session.

ProxySession is the SDK class specifically designed to streamline proxy session creation. The source code example below shows how to override the authenticate method for use in a CORS request scenario.

  • getProxyToken() is the call to the proxy server's API that returns the API auth token to use
  • the code in the if (this.isAuthenticated() branch
    • Sets CORS mode
    • Sets the auth token header
    • Identifies the Looker SDK version for the Looker server

By writing your own getProxyToken() visible to this class, any proxied authentication workflow is supported.

export class EmbedSession extends ProxySession {
  constructor(public settings: IApiSettings, transport?: ITransport) {
    super(settings, transport);
  }

  async authenticate(props: any) {
    // get the auth token from the proxy server
    const token = await getProxyToken();
    if (token) {
      // Assign the token, which will track its expiration time automatically
      this.activeToken.setToken(token);
    }

    if (this.isAuthenticated()) {
      // Session is authenticated
      // set CORS mode (in this scenario)
      props.mode = 'cors';

      // remove any credentials attribute that may have been set
      // because the BrowserTransport defaults to having `same-origin` for credentials
      delete props['credentials'];

      // replace the headers argument with required values
      // Note: using new Headers() to construct the headers breaks CORS for the Looker API. Don't know why yet
      props.headers = {
        Authorization: `Bearer ${token.access_token}`,
        'x-looker-appid': agentTag,
      };
    }
    return props;
  }
}

More examples

Looker's open source repository of SDK Examples has more example scripts and applications that show how to use the various Looker language SDKs.

A note about security

Any script or configuration file used to provide credentials to your Looker SDK instance needs to be secured.