# @lekko/next-sdk

> Lekko Next.js SDK

Latest version **0.3.0-beta.6** (published 2024-10-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install @lekko/next-sdk
pnpm add @lekko/next-sdk
yarn add @lekko/next-sdk
bun add @lekko/next-sdk
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.0-beta.6 |
| Published | 2024-10-09 |
| First published | 2024-04-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 4 |
| Unpacked size | 31.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | passichenko, lekko_jonathan, davidsgk, konradlekko |
| Keywords | lekko, typescript, next |

## Links

- npm: https://www.npmjs.com/package/@lekko/next-sdk
- Repository: https://github.com/lekkodev/ts-tools
- Homepage: https://github.com/lekkodev/ts-tools#readme
- Issues: https://github.com/lekkodev/ts-tools/issues
- npm.io page: https://npm.io/package/@lekko/next-sdk

## Dependencies (4)

- [js-base64](https://npm.io/package/js-base64.md) ^3.7.7
- [@lekko/js-sdk](https://npm.io/package/@lekko/js-sdk.md) ^0.1.7 || >=0.1.8-beta.1
- [@connectrpc/connect](https://npm.io/package/@connectrpc/connect.md) ^1.4.0
- [@lekko/webpack-loader](https://npm.io/package/@lekko/webpack-loader.md) ^0.2.0 || >=0.2.1-beta.1

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.3.0-beta.6 (latest) — 2024-10-09
- 0.3.0-beta.3 — 2024-10-09
- 0.3.0-beta.2 — 2024-07-11
- 0.3.0-beta.1 — 2024-07-05
- 0.2.6 — 2024-07-01
- 0.2.5 — 2024-05-17
- 0.2.4 — 2024-04-22
- 0.2.3 — 2024-04-11
- 0.2.2 — 2024-04-11
- 0.2.1 — 2024-04-11
- 0.2.0 — 2024-04-05
- 0.1.2 — 2024-04-05
- 0.1.1 — 2024-04-03
- 0.1.0 — 2024-04-03

## README

# @lekko/next-sdk

The Lekko Next.js SDK allows you to use dynamic configuration in your Next.js project via lekkos.

## Usage

In your `next.config.js`, add the `withLekkoNextConfig` helper wrapper. This will perform build-time checks and transformations for your lekkos.

Note that this wrapper is imported from `@lekko/next-sdk/config` and not just `@lekko/next-sdk`.

```typescript
const { withLekkoNextConfig } = require("@lekko/next-sdk/config");

const nextConfig = {
  // Your regular Next.js configs go here
};

module.exports = withLekkoNextConfig(nextConfig);
```

In any client component, use the `useLekkoConfig` hook:

```typescript
"use client";

import { useLekkoConfig } from "@lekko/next-sdk";
// User-defined config functions
import { getSomeConfig } from "@/lekko/default";

...

export default function MyClientComponent() {
  // First arg is the config function to be evaluation, second arg is the evaluation context
  const config = useLekkoConfig(getSomeConfig, {});

  return (
    ...
  );
}
```

Example lekkos:

```typescript
// lekko/default.ts

/** Description of some config */
export function getSomeConfig(): string {
  return "Hi, I'm a config function!";
}

/** This is a feature flag used somewhere in the app */
export function getMyFeatureFlag({ userId }: { userId: number }): boolean {
  if (userId === 15) {
    return true;
  }
  return false;
}
```

### App Router

In `app/layout.tsx` (or a similar top-level layout):

```typescript
import { LekkoNextProvider } from "@lekko/next-sdk";

...

export default function RootLayout({ children }: Readonly<{ children: React.ReactNode }>) {
  return (
    <html lang="en">
      <body>
        <LekkoNextProvider revalidate={10}>
          {children}
        </LekkoNextProvider>
      </body>
    </html>
  );
}
```

The `LekkoNextProvider` is responsible for connecting to Lekko's services and hydrating client-side code with up-to-date config values.

It makes the `useLekkoConfig` hook available to use in client components.

### Pages Router

In `pages/_app.tsx` (or a similar top-level layout):

```typescript
import { LekkoClientProvider } from "@lekko/next-sdk";

...

export default function App({ Component, pageProps }: AppProps) {
  return (
    // We populate the `lekkoConfigs` prop below
    <LekkoClientProvider configs={pageProps.lekkoConfigs}>
      <Component {...pageProps} />
    </LekkoClientProvider>
  );
}
```

Then, in each page you want to use the `useLekkoConfig` hook under:

```typescript
import { useLekkoConfig, withLekkoServerSideProps } from "@lekko/next-sdk";
// User-defined config functions
import { getSomeConfig } from "@/lekko/default";

export default function SomePage() {
  const config = useLekkoConfig(getSomeConfig, {});

  return (...);
}

// Wrap your custom getServerSideProps
export const getServerSideProps = withLekkoServerSideProps(...);
// Alternatively, for statically rendered pages...
export const getStaticProps = withLekkoStaticProps(...);
```

Note if a page doesn't receive the config contents, its sub-component tree will not be able to use dynamic production values of Lekko configs and will use the static fallback instead.

## Environment variables

To connect your project to Lekko's services when building (e.g. when deploying), pass the `NEXT_PUBLIC_LEKKO_API_KEY` environment variable.

You can generate API keys for your team on the Lekko [web UI](https://app.lekko.com) by clicking on your team in the top navigation bar -> Admin tab -> API keys.

---
_Source: https://npm.io/package/@lekko/next-sdk · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
