Skip to content

Commit

Permalink
feat: add node server package (#102)
Browse files Browse the repository at this point in the history
* feat: add node server package

* add docs

* use supertest
cstrnt authored Dec 17, 2023
1 parent 0a14923 commit 631e914
Showing 39 changed files with 1,623 additions and 405 deletions.
7 changes: 7 additions & 0 deletions apps/angular-example/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# angular-example

## 0.0.15

### Patch Changes

- @tryabby/angular@2.0.5
- @tryabby/devtools@5.0.0

## 0.0.14

### Patch Changes
2 changes: 1 addition & 1 deletion apps/angular-example/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-example",
"version": "0.0.14",
"version": "0.0.15",
"private": true,
"scripts": {
"ng": "ng",
35 changes: 23 additions & 12 deletions apps/docs/pages/config.mdx
Original file line number Diff line number Diff line change
@@ -33,9 +33,9 @@ export default defineConfig(
```

<Callout type="info" emoji="💡">
The first parameter includes all data that might be configured via environment variables.
You should not use environment variables in the second parameter, because the type inference
might suffer and the CLI could potentially resolve those environment variables!
The first parameter includes all data that might be configured via environment variables. You
should not use environment variables in the second parameter, because the type inference might
suffer and the CLI could potentially resolve those environment variables!
</Callout>

## Philosophy
@@ -107,15 +107,17 @@ export default defineConfig(
The `defineConfig` function takes two objects as a parameter, that will be merged together when read.
The merged object can contain the following properties:

| Name | Type | Required | Description | details |
| ------------------ | -------- | :------: | -------------------------------------------------------- | --------------------- |
| projectId | `string` || The ID of your project in Abby | - |
| apiUrl | `string` | | The URL of the Abby API. Defaults to the hosted version | - |
| currentEnvironment | `string` || The current environment of your application | [link](/environments) |
| tests | `object` | | An object containing your defined A/B Tests | - |
| flags | `array` | | An array containing your defined Feature Flag names | - |
| remoteConfig | `object` | | An object containing your Remote Configuration variables | - |
| settings | `object` | | An object with additional settings for Abby | - |
| Name | Type | Required | Description | details |
| ------------------ | --------- | :------: | ----------------------------------------------------------------------- | --------------------- |
| projectId | `string` || The ID of your project in Abby | - |
| apiUrl | `string` | | The URL of the Abby API. Defaults to the hosted version | - |
| currentEnvironment | `string` || The current environment of your application | [link](/environments) |
| tests | `object` | | An object containing your defined A/B Tests | - |
| flags | `array` | | An array containing your defined Feature Flag names | - |
| remoteConfig | `object` | | An object containing your Remote Configuration variables | - |
| settings | `object` | | An object with additional settings for Abby | - |
| debug | `boolean` | | A boolean to show additional debug information | - |
| fetch | `fetch` | | You can pass in a custom fetch function. Defaults to `globalThis.fetch` | - |

#### tests

@@ -217,3 +219,12 @@ The settings property is an object containing additional settings for Abby. The
},
},
```

#### debug

A boolean that you can toggle on to get debug information logged to the console. Mostly useful in development to see where something potentially goes wrong.

#### fetch

You can pass a custom fetch function in here. This defaults to the global fetch function. This is mostly needed for older versions of Node.js which
don't have a global fetch function.
9 changes: 6 additions & 3 deletions apps/docs/pages/integrations/_meta.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"nextjs": "Next.js",
"react": "React",
"svelte": "Svelte",
"angular": "Angular"
}
"svelte": "Svelte",
"angular": "Angular",
"node": "Node",
"express": "Express",
"koa": "Koa"
}
140 changes: 140 additions & 0 deletions apps/docs/pages/integrations/express.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import { Tab, Tabs, Callout } from "nextra-theme-docs";

# Express

Our Express package allows you to use Abby within all server-side applications.
If you're using Node < 18 you will need to also install any fetch polyfill.

## Installation

To get started make sure to install the packages using your favorite package manager.

<Tabs items={["npm", "yarn", "pnpm"]}>
<Tab>
```bash
npm i @tryabby/express
```
</Tab>

<Tab>
```bash
yarn add @tryabby/express
```
</Tab>
<Tab>
```bash
pnpm i @tryabby/express
```
</Tab>
</Tabs>

## Usage

### Create your config

To use Abby you need to create your config first. You can do this by creating a file called `abby.config.ts` in your root folder. This file will be used to configure your project.

```ts
// abby.config.ts
import { defineConfig } from "@tryabby/node";

export default defineConfig({
projectId: "<YOUR_PROJECT_ID>",
currentEnvironment: environment.production ? "production" : "development",
environments: ["production", "development"],
tests: {
test: { variants: ["A", "B"] },
footer: { variants: ["dark", "orange", "green"] },
// ... your tests
},
flags: ["darkMode", "newFeature"],
remoteConfig: {
customButtonText: "String",
maxSessionCount: "Number",
},
});
```

### Create your Abby instance

To use Abby in your code you need to call the `createAbby` function

```ts
import { createAbby } from "@tryabby/express";
import abbyConfig from "../abby.config";

const { abby, middleware } = createAbby(abbyConfig);
```

### Using Abby in your code

You can now use Abby in your code. Before you can use Abby inside of your route handlers you will need to add the middleware to your Express app.

#### Middleware

```ts
import express from "express";
import { createAbby } from "@tryabby/express";
import abbyConfig from "../abby.config";

const { abby, middleware } = createAbby(abbyConfig);

const app = express();

app.use(middleware);

app.get("/", (req, res) => {
const testVariant = abby.getTestVariant("test");

if (testVariant === "A") {
// show variant A
} else {
// show variant B
}
});

app.listen(3000, () => {
console.log("Server listening on port 3000");
});
```

#### Feature Flags

You can call the `getFeatureFlag` function to get the value of a feature flag.
Since you loaded all the information from our API this is a synchronous function.

```ts
if (abby.getFeatureFlag("newFeature")) {
// use new algorithm
} else {
// use old algorithm
}
```

#### Remote Configs

You can call the `getRemoteConfig` function to get the value of a remote config.

```ts
const maxSessionCount = abby.getRemoteConfig("maxSessionCount");

if (sessionCount < maxSessionCount) {
// show session
} else {
// show error
}
```

#### A/B Tests

You can call the `getTestVariant` function to get the variant of a test.

```ts
const testVariant = abby.getTestVariant("test");

if (testVariant === "A") {
// show variant A
} else {
// show variant B
}
```
132 changes: 132 additions & 0 deletions apps/docs/pages/integrations/koa.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import { Tab, Tabs, Callout } from "nextra-theme-docs";

# Koa

Our Koa package allows you to use Abby within all server-side applications.
If you're using Node < 18 you will need to also install any fetch polyfill.

## Installation

To get started make sure to install the packages using your favorite package manager.

<Tabs items={["npm", "yarn", "pnpm"]}>
<Tab>
```bash
npm i @tryabby/koa
```
</Tab>

<Tab>
```bash
yarn add @tryabby/koa
```
</Tab>
<Tab>
```bash
pnpm i @tryabby/koa
```
</Tab>
</Tabs>

## Usage

### Create your config

To use Abby you need to create your config first. You can do this by creating a file called `abby.config.ts` in your root folder. This file will be used to configure your project.

```ts
// abby.config.ts
import { defineConfig } from "@tryabby/node";

export default defineConfig({
projectId: "<YOUR_PROJECT_ID>",
currentEnvironment: environment.production ? "production" : "development",
environments: ["production", "development"],
tests: {
test: { variants: ["A", "B"] },
footer: { variants: ["dark", "orange", "green"] },
// ... your tests
},
flags: ["darkMode", "newFeature"],
remoteConfig: {
customButtonText: "String",
maxSessionCount: "Number",
},
});
```

### Create your Abby instance

To use Abby in your code you need to call the `createAbby` function

```ts
import { createAbby } from "@tryabby/koa";
import abbyConfig from "../abby.config";

const { abby, middleware } = createAbby(abbyConfig);
```

### Using Abby in your code

You can now use Abby in your code. Before you can use Abby inside of your route handlers you will need to add the middleware to your Koa app.

#### Middleware

```ts
import Koa from "Koa";
import { createAbby } from "@tryabby/koa";
import abbyConfig from "../abby.config";

const { abby, middleware } = createAbby(abbyConfig);

const app = new Koa();

app.use(middleware);

// ... your routes

app.listen(3000, () => {
console.log("Server listening on port 3000");
});
```

#### Feature Flags

You can call the `getFeatureFlag` function to get the value of a feature flag.
Since you loaded all the information from our API this is a synchronous function.

```ts
if (abby.getFeatureFlag("newFeature")) {
// use new algorithm
} else {
// use old algorithm
}
```

#### Remote Configs

You can call the `getRemoteConfig` function to get the value of a remote config.

```ts
const maxSessionCount = abby.getRemoteConfig("maxSessionCount");

if (sessionCount < maxSessionCount) {
// show session
} else {
// show error
}
```

#### A/B Tests

You can call the `getTestVariant` function to get the variant of a test.

```ts
const testVariant = abby.getTestVariant("test");

if (testVariant === "A") {
// show variant A
} else {
// show variant B
}
```
Loading

2 comments on commit 631e914

@vercel
Copy link

@vercel vercel bot commented on 631e914 Dec 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 631e914 Dec 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.