@flagify/astro 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +197 -19
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -1,16 +1,72 @@
1
- # @flagify/astro
1
+ <p align="center">
2
+ <a href="https://flagify.dev">
3
+ <img alt="Flagify" src="https://flagify.dev/logo-color.svg" width="280" />
4
+ </a>
5
+ </p>
6
+
7
+ <p align="center">
8
+ <strong>Feature flags for modern teams</strong>
9
+ </p>
10
+
11
+ <p align="center">
12
+ <a href="https://www.npmjs.com/package/@flagify/astro"><img src="https://img.shields.io/npm/v/@flagify/astro.svg?style=flat-square&color=0D80F9" alt="npm version" /></a>
13
+ <a href="https://www.npmjs.com/package/@flagify/astro"><img src="https://img.shields.io/npm/dm/@flagify/astro.svg?style=flat-square&color=0D80F9" alt="npm downloads" /></a>
14
+ <a href="https://github.com/flagifyhq/javascript/blob/main/LICENSE"><img src="https://img.shields.io/npm/l/@flagify/astro.svg?style=flat-square&color=0D80F9" alt="license" /></a>
15
+ <a href="https://github.com/flagifyhq/javascript"><img src="https://img.shields.io/github/stars/flagifyhq/javascript?style=flat-square&color=0D80F9" alt="github stars" /></a>
16
+ </p>
17
+
18
+ <p align="center">
19
+ <a href="https://flagify.dev/docs">Documentation</a> &middot;
20
+ <a href="https://flagify.dev/docs/sdks/astro">SDK Reference</a> &middot;
21
+ <a href="https://github.com/flagifyhq/javascript/issues">Issues</a> &middot;
22
+ <a href="https://flagify.dev">Website</a>
23
+ </p>
2
24
 
3
- Official [Flagify](https://flagify.dev) integration for [Astro](https://astro.build). Evaluate feature flags in Astro pages and components with a dev toolbar for overrides.
25
+ ---
26
+
27
+ ## Overview
28
+
29
+ `@flagify/astro` is the official Astro integration for [Flagify](https://flagify.dev). Evaluate feature flags in Astro pages and components with a dev toolbar for overrides and a Vercel Flags SDK adapter.
30
+
31
+ - **Astro-native** -- Integration with auto-injected middleware and dev toolbar
32
+ - **`defineFlag` API** -- Declare flags with typed defaults and options
33
+ - **Dev toolbar** -- Toggle flag overrides during development via cookie persistence
34
+ - **Vercel Flags SDK** -- Built-in adapter via `@flagify/astro/adapter`
35
+ - **SSR + SSG** -- Works in both server-rendered and static builds
36
+ - **Lightweight** -- Thin wrapper over [`@flagify/node`](https://www.npmjs.com/package/@flagify/node)
37
+
38
+ ## Table of contents
39
+
40
+ - [Installation](#installation)
41
+ - [Quick start](#quick-start)
42
+ - [Define flags](#define-flags)
43
+ - [Evaluate in pages](#evaluate-in-pages)
44
+ - [Flag evaluation priority](#flag-evaluation-priority)
45
+ - [Dev toolbar](#dev-toolbar)
46
+ - [Vercel Flags SDK adapter](#vercel-flags-sdk-adapter)
47
+ - [Client management](#client-management)
48
+ - [SSG limitations](#ssg-limitations)
49
+ - [TypeScript](#typescript)
50
+ - [API reference](#api-reference)
51
+ - [Contributing](#contributing)
52
+ - [License](#license)
4
53
 
5
54
  ## Installation
6
55
 
7
56
  ```bash
8
- npm install @flagify/astro @flagify/node
9
- # or
57
+ # pnpm
10
58
  pnpm add @flagify/astro @flagify/node
59
+
60
+ # npm
61
+ npm install @flagify/astro @flagify/node
62
+
63
+ # yarn
64
+ yarn add @flagify/astro @flagify/node
11
65
  ```
12
66
 
13
- ## Setup
67
+ > **Peer dependency:** Astro >= 4.0.0
68
+
69
+ ## Quick start
14
70
 
15
71
  ### 1. Add environment variables
16
72
 
@@ -37,9 +93,7 @@ This automatically registers:
37
93
  - **Middleware** that initializes the Flagify client and parses override cookies
38
94
  - **Dev toolbar app** for toggling flag overrides during development
39
95
 
40
- ## Usage
41
-
42
- ### Define flags
96
+ ### 3. Define and evaluate flags
43
97
 
44
98
  ```ts
45
99
  // src/flags.ts
@@ -50,7 +104,34 @@ export const newCheckout = defineFlag({
50
104
  description: 'New checkout flow experience',
51
105
  default: false,
52
106
  });
107
+ ```
53
108
 
109
+ ```astro
110
+ ---
111
+ // src/pages/index.astro
112
+ import { newCheckout } from '../flags';
113
+
114
+ const isNewCheckout = await newCheckout(Astro);
115
+ ---
116
+
117
+ {isNewCheckout && <NewCheckoutBanner />}
118
+ ```
119
+
120
+ ## Define flags
121
+
122
+ Use `defineFlag` to declare feature flags with typed defaults and optional variant options.
123
+
124
+ ```ts
125
+ import { defineFlag } from '@flagify/astro';
126
+
127
+ // Boolean flag
128
+ export const darkMode = defineFlag({
129
+ key: 'dark-mode',
130
+ description: 'Enable dark mode',
131
+ default: false,
132
+ });
133
+
134
+ // String variant with options (used by the dev toolbar)
54
135
  export const heroVariant = defineFlag({
55
136
  key: 'hero-variant',
56
137
  description: 'A/B test for hero section',
@@ -60,34 +141,44 @@ export const heroVariant = defineFlag({
60
141
  { value: 'variant-a', label: 'Variant A' },
61
142
  ],
62
143
  });
144
+
145
+ // Number flag
146
+ export const maxItems = defineFlag({
147
+ key: 'max-items',
148
+ description: 'Maximum items per page',
149
+ default: 20,
150
+ });
63
151
  ```
64
152
 
65
- ### Evaluate in pages/components
153
+ ## Evaluate in pages
154
+
155
+ Each flag defined with `defineFlag` returns an async function that accepts the Astro global.
66
156
 
67
157
  ```astro
68
158
  ---
69
- // src/pages/index.astro
70
- import { newCheckout, heroVariant } from '../flags';
159
+ import { newCheckout, heroVariant, maxItems } from '../flags';
71
160
 
72
161
  const isNewCheckout = await newCheckout(Astro);
73
162
  const hero = await heroVariant(Astro);
163
+ const max = await maxItems(Astro);
74
164
  ---
75
165
 
76
166
  {isNewCheckout && <NewCheckoutBanner />}
77
167
  <Hero variant={hero} />
168
+ <ItemList max={max} />
78
169
  ```
79
170
 
80
- ### Flag evaluation priority
171
+ ## Flag evaluation priority
81
172
 
82
- 1. **Override cookie** dev overrides from the toolbar take highest priority
83
- 2. **Flagify SDK** evaluated via `@flagify/node` (cached locally)
84
- 3. **Default value** the `default` from `defineFlag()`
173
+ 1. **Override cookie** -- dev overrides from the toolbar take highest priority
174
+ 2. **Flagify SDK** -- evaluated via `@flagify/node` (cached locally)
175
+ 3. **Default value** -- the `default` from `defineFlag()`
85
176
 
86
- ## Dev Toolbar
177
+ ## Dev toolbar
87
178
 
88
179
  In development mode, the Flagify toolbar app lets you set flag overrides as JSON. Overrides are stored in a `flagify-overrides` cookie and persist across page navigations.
89
180
 
90
- ## Vercel Flags SDK Adapter
181
+ ## Vercel Flags SDK adapter
91
182
 
92
183
  For projects using the [Vercel Flags SDK](https://flags-sdk.dev):
93
184
 
@@ -103,7 +194,40 @@ export const checkout = flag({
103
194
  });
104
195
  ```
105
196
 
106
- ## SSG Limitations
197
+ The adapter supports user targeting when entities are passed:
198
+
199
+ ```ts
200
+ const { adapter } = createFlagifyAdapter({ origin: 'https://app.flagify.dev' });
201
+
202
+ export const premium = flag({
203
+ key: 'premium-feature',
204
+ adapter: adapter('premium-feature'),
205
+ defaultValue: false,
206
+ });
207
+ ```
208
+
209
+ ## Client management
210
+
211
+ The integration manages a singleton `@flagify/node` client. For advanced use cases, you can control it directly:
212
+
213
+ ```ts
214
+ import { initClient, getClient, waitForClient, destroyClient } from '@flagify/astro';
215
+
216
+ // Initialize (no-op if already initialized)
217
+ initClient({ projectKey: 'proj_xxx', publicKey: 'pk_xxx' });
218
+
219
+ // Wait for initial flag sync
220
+ await waitForClient();
221
+
222
+ // Access the client
223
+ const client = getClient();
224
+ client?.isEnabled('my-flag');
225
+
226
+ // Cleanup
227
+ destroyClient();
228
+ ```
229
+
230
+ ## SSG limitations
107
231
 
108
232
  In static builds (SSG), flags are evaluated at **build time** without user context. This works for global flags (kill switches, percentage rollouts) but not for user-targeted flags. For user-targeted evaluation, use SSR mode.
109
233
 
@@ -122,6 +246,60 @@ declare namespace App {
122
246
  }
123
247
  ```
124
248
 
249
+ ## API reference
250
+
251
+ ### Main entrypoint (`@flagify/astro`)
252
+
253
+ | Export | Type | Description |
254
+ |--------|------|-------------|
255
+ | `default` (flagify) | Function | Astro integration -- registers middleware and dev toolbar |
256
+ | `defineFlag` | Function | Define a feature flag with typed defaults |
257
+ | `initClient` | Function | Initialize the singleton Flagify client |
258
+ | `getClient` | Function | Get the current client instance (or null) |
259
+ | `waitForClient` | Function | Wait for initial flag sync to complete |
260
+ | `destroyClient` | Function | Destroy the client and free resources |
261
+ | `FlagifyAstroOptions` | Type | Integration options (extends `FlagifyOptions`) |
262
+ | `FlagDefinition` | Type | Shape of a flag definition |
263
+ | `FlagifyLocals` | Type | Shape of `context.locals` set by middleware |
264
+ | `FlagEvaluator` | Type | Return type of `defineFlag()` |
265
+
266
+ ### Middleware (`@flagify/astro/middleware`)
267
+
268
+ | Export | Type | Description |
269
+ |--------|------|-------------|
270
+ | `onRequest` | Function | Astro middleware -- inits client and parses override cookies |
271
+
272
+ ### Adapter (`@flagify/astro/adapter`)
273
+
274
+ | Export | Type | Description |
275
+ |--------|------|-------------|
276
+ | `createFlagifyAdapter` | Function | Creates a Vercel Flags SDK compatible adapter |
277
+
278
+ ## Contributing
279
+
280
+ We welcome contributions. Please open an issue first to discuss what you'd like to change.
281
+
282
+ ```bash
283
+ # Clone
284
+ git clone https://github.com/flagifyhq/javascript.git
285
+ cd javascript
286
+
287
+ # Install
288
+ pnpm install
289
+
290
+ # Development (watch mode)
291
+ pnpm run dev
292
+
293
+ # Build
294
+ pnpm run build
295
+ ```
296
+
125
297
  ## License
126
298
 
127
- MIT
299
+ MIT -- see [LICENSE](./LICENSE) for details.
300
+
301
+ ---
302
+
303
+ <p align="center">
304
+ <sub>Built with care by the <a href="https://flagify.dev">Flagify</a> team</sub>
305
+ </p>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flagify/astro",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "Astro integration for Flagify — feature flags with dev toolbar and middleware.",
5
5
  "author": "Mario Campbell R <mario@mariocampbellr.com>",
6
6
  "license": "MIT",
@@ -29,7 +29,7 @@
29
29
  "dist"
30
30
  ],
31
31
  "dependencies": {
32
- "@flagify/node": "1.0.0"
32
+ "@flagify/node": "1.0.2"
33
33
  },
34
34
  "peerDependencies": {
35
35
  "astro": ">=4.0.0"