@flagsync/nextjs-sdk 0.2.6
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.
- package/LICENSE +201 -0
- package/README.md +288 -0
- package/dist/index.cjs +89 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +34 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +59 -0
- package/dist/index.js.map +1 -0
- package/package.json +68 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
# FlagSync Adapter for Vercel Flags
|
|
2
|
+
|
|
3
|
+
[FlagSync](https://www.flagsync.com) brings affordable, user-friendly feature flags and A/B testing to indie hackers and growing businesses. This adapter integrates FlagSync with [Vercel Flags](https://vercel.com/docs/flags) in Next.js apps. [Get started](https://docs.flagsync.com/getting-started/set-up-flagsync) today!
|
|
4
|
+
|
|
5
|
+
[](https://badge.fury.io/js/%40flagsync%2Fvercel-flags-sdk)
|
|
6
|
+
[](https://twitter.com/flagsync)
|
|
7
|
+
[](https://www.apache.org/licenses/LICENSE-2.0)
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## Getting Started
|
|
12
|
+
|
|
13
|
+
Refer to the [SDK documentation](https://docs.flagsync.com/sdks/next.js) for more information on how to use this library.
|
|
14
|
+
|
|
15
|
+
### Compatibility
|
|
16
|
+
|
|
17
|
+
- Requires [Next.js](https://nextjs.org/) 14+
|
|
18
|
+
- Compatible with Node.js 16+ and ES5.
|
|
19
|
+
- TypeScript is fully supported.
|
|
20
|
+
- **Note:** This SDK is optimized for Next.js App Router. For non-Next.js Node.js apps, use the [FlagSync Node SDK](https://github.com/flagsync/node-client) instead.
|
|
21
|
+
|
|
22
|
+
### Prerequisites
|
|
23
|
+
- Install the SDK: `npm install @flagsync/vercel-flags-sdk flags/next`
|
|
24
|
+
- Set the `FLAGSYNC_SDK_KEY` environment variable in your `.env` file.
|
|
25
|
+
|
|
26
|
+
## Basic Setup
|
|
27
|
+
|
|
28
|
+
FlagSync lets you toggle features and personalize experiences in your Next.js App Router application. Follow these steps to set it up.
|
|
29
|
+
|
|
30
|
+
### Step 1: Create the FlagSync client singleton
|
|
31
|
+
|
|
32
|
+
Initialize the client with your server-side SDK key:
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
// lib/flagsync.ts
|
|
36
|
+
import { createFlagSyncClient } from '@flagsync/vercel-flags-sdk';
|
|
37
|
+
|
|
38
|
+
export const client = createFlagSyncClient({
|
|
39
|
+
sdkKey: process.env.FLAGSYNC_SDK_KEY!,
|
|
40
|
+
});
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Step 2: Set up user identification
|
|
44
|
+
|
|
45
|
+
> [!NOTE]
|
|
46
|
+
> Feature flags in FlagSync use user context (e.g. via `identify`) to enable personalized experiences.
|
|
47
|
+
|
|
48
|
+
The `identify` function defines how users are identified (e.g., via cookies, headers, or session data). See [Identify](#identify) for more details.
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
// lib/flagsync.ts
|
|
52
|
+
import {
|
|
53
|
+
createIdentify,
|
|
54
|
+
createFlagSyncClient,
|
|
55
|
+
} from '@flagsync/vercel-flags-sdk';
|
|
56
|
+
|
|
57
|
+
export const identify = createIdentify(({ cookies }) => {
|
|
58
|
+
const userId = cookies.get('user-id')?.value; // Authenticated user ID
|
|
59
|
+
const visitorId = cookies.get('visitor-id')?.value; // Unauthenticated visitor ID
|
|
60
|
+
return {
|
|
61
|
+
key: userId ?? visitorId ?? 'anonymous' // Fallback to 'anonymous'
|
|
62
|
+
};
|
|
63
|
+
});
|
|
64
|
+
```
|
|
65
|
+
> [!CAUTION]
|
|
66
|
+
> While `anonymous` user contexts are supported, always provide a unique identifier (even for unauthenticated users) to ensure consistent flag evaluation.
|
|
67
|
+
>
|
|
68
|
+
> See [Identify](#identify) for more details.
|
|
69
|
+
|
|
70
|
+
### Step 3: Define a feature flag
|
|
71
|
+
|
|
72
|
+
Create a flag definition using the shared `client` and `identify` function:
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
// app/dashboard/flags.ts
|
|
76
|
+
import { flag } from 'flags/next';
|
|
77
|
+
import { client, identify } from '@/lib/flagsync';
|
|
78
|
+
import { createStringFlagAdaptor } from '@flagsync/vercel-flags-sdk';
|
|
79
|
+
|
|
80
|
+
export const dashboardFlag = flag<string>({
|
|
81
|
+
identify, // Links flag to user context
|
|
82
|
+
key: 'dashboardFlag', // Unique flag key
|
|
83
|
+
adapter: createStringFlagAdaptor(client), // Retrieves the flag with type safety
|
|
84
|
+
});
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### Step 4: Use the flag in your app
|
|
88
|
+
|
|
89
|
+
Now you can use `dashboardFlag` in your Server Components:
|
|
90
|
+
|
|
91
|
+
```typescript jsx
|
|
92
|
+
// app/dashboard/page.tsx
|
|
93
|
+
|
|
94
|
+
import { dashboardFlag } from '@/app/dashboard/flags';
|
|
95
|
+
|
|
96
|
+
export default async function DashboardPage() {
|
|
97
|
+
const dashboard = await dashboardFlag();
|
|
98
|
+
|
|
99
|
+
return (
|
|
100
|
+
<div>The value of {dashboardFlag.key} is {dashboard}</div>
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Type-Safe Feature Flags
|
|
106
|
+
|
|
107
|
+
FlagSync supports multiple flag types for different use cases. Use the type-specific adapter depending on your flag type:
|
|
108
|
+
|
|
109
|
+
- `createBoolFlagAdaptor`: For boolean flags
|
|
110
|
+
- `createStringFlagAdaptor`: For string flags
|
|
111
|
+
- `createNumberFlagAdaptor`: For numeric flags
|
|
112
|
+
- `createJsonFlagAdaptor`: For JSON object flags
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
// app/<route>/flags.ts
|
|
116
|
+
import { client, identify } from '@/lib/flagsync';
|
|
117
|
+
import {
|
|
118
|
+
createBoolFlagAdaptor,
|
|
119
|
+
createJsonFlagAdaptor,
|
|
120
|
+
createNumberFlagAdaptor,
|
|
121
|
+
createStringFlagAdaptor,
|
|
122
|
+
} from '@flagsync/vercel-flags-sdk';
|
|
123
|
+
import { flag } from '@vercel/flags';
|
|
124
|
+
|
|
125
|
+
// Boolean flag for feature toggles
|
|
126
|
+
export const betaFeatureFlag = flag<boolean>({
|
|
127
|
+
identify,
|
|
128
|
+
key: 'beta-feature-enabled',
|
|
129
|
+
adapter: createBoolFlagAdaptor(client),
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// String flag for UI variants
|
|
133
|
+
export const heroCtaFlag = flag<string>({
|
|
134
|
+
identify,
|
|
135
|
+
key: 'hero-cta',
|
|
136
|
+
adapter: createStringFlagAdaptor(client),
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
// Number flag for pricing tiers
|
|
140
|
+
export const discountFlag = flag<number>({
|
|
141
|
+
identify,
|
|
142
|
+
key: 'price-discount',
|
|
143
|
+
adapter: createNumberFlagAdaptor(client),
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
// JSON flag for complex configurations
|
|
147
|
+
export const featureConfigFlag = flag<{
|
|
148
|
+
enabled: boolean;
|
|
149
|
+
limits: { requests: number; storage: number };
|
|
150
|
+
}>({
|
|
151
|
+
identify,
|
|
152
|
+
key: 'feature-config',
|
|
153
|
+
adapter: createJsonFlagAdaptor(client),
|
|
154
|
+
});
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## Identify
|
|
158
|
+
|
|
159
|
+
The `identify` function links feature flags to user contexts in FlagSync by returning an `FsUserContext` object.
|
|
160
|
+
|
|
161
|
+
```typescript
|
|
162
|
+
type FsUserContext = {
|
|
163
|
+
key: string; // Unique identifier for the user
|
|
164
|
+
attributes?: Record<string, string | number | boolean>; // Optional custom data (used in individual targeting)
|
|
165
|
+
};
|
|
166
|
+
```
|
|
167
|
+
* `key`: A unique ID for the user (required)
|
|
168
|
+
* `attributes`: Optional metadata (e.g., user agent, region, role, department) to personalize flag evaluations.
|
|
169
|
+
|
|
170
|
+
> [!IMPORTANT]
|
|
171
|
+
> User contexts enable personalized flag evaluations via [Individual Targeting](https://docs.flagsync.com/sdks/sdk-concepts/flag-evaluation#does-the-flag-have-any-individual-targeting-rules), and consistent experiences during [Percentage Rollouts](https://docs.flagsync.com/sdks/sdk-concepts/flag-evaluation#does-the-flag-have-a-percentage-rollout).
|
|
172
|
+
|
|
173
|
+
We recommend:
|
|
174
|
+
1. Using a helper function (getFlagSyncUserContext) to build the full context.
|
|
175
|
+
2. Setting cookies in middleware for the key.
|
|
176
|
+
|
|
177
|
+
Here's how:
|
|
178
|
+
|
|
179
|
+
### Step 1: Define a helper function
|
|
180
|
+
|
|
181
|
+
Create a utility to build the user context:
|
|
182
|
+
|
|
183
|
+
```typescript
|
|
184
|
+
// lib/flagsync.user-context.ts
|
|
185
|
+
import type { ReadonlyRequestCookies, ReadonlyHeaders } from 'flags';
|
|
186
|
+
import { dedupe } from 'flags/next';
|
|
187
|
+
import { nanoid } from 'nanoid';
|
|
188
|
+
import type { NextRequest } from 'next/server';
|
|
189
|
+
|
|
190
|
+
const generateId = dedupe(async () => nanoid());
|
|
191
|
+
|
|
192
|
+
export const getFlagSyncUserContext = async (
|
|
193
|
+
cookies: ReadonlyRequestCookies | NextRequest['cookies'],
|
|
194
|
+
headers: ReadonlyHeaders | NextRequest['headers'],
|
|
195
|
+
) => {
|
|
196
|
+
const userId = cookies.get('user-id')?.value; // Authenticated user
|
|
197
|
+
const visitorId = cookies.get('visitor-id')?.value; // Anonymous visitor
|
|
198
|
+
|
|
199
|
+
return {
|
|
200
|
+
key: userId ?? visitorId ?? (await generateId()), // Fallback to new ID
|
|
201
|
+
attributes: {
|
|
202
|
+
userAgent: headers.get('user-agent') || 'unknown', // Browser info
|
|
203
|
+
region: headers.get('x-region-code') || 'default', // Custom header
|
|
204
|
+
},
|
|
205
|
+
};
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
```
|
|
209
|
+
### Step 2: Set up identification
|
|
210
|
+
|
|
211
|
+
Use the helper in `identify`:
|
|
212
|
+
|
|
213
|
+
```typescript
|
|
214
|
+
// lib/flagsync.ts
|
|
215
|
+
import { createIdentify, createFlagSyncClient } from '@flagsync/vercel-flags-sdk';
|
|
216
|
+
import { getFlagSyncUserContext } from '@lib/flagsync.user-context';
|
|
217
|
+
|
|
218
|
+
export const identify = createIdentify(({ cookies, headers }) => {
|
|
219
|
+
return getFlagSyncUserContext(cookies, headers);
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
export const client = createFlagSyncClient({
|
|
223
|
+
sdkKey: process.env.FLAGSYNC_SDK_KEY!,
|
|
224
|
+
});
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
### Step 3: Handle cookies in middleware
|
|
228
|
+
|
|
229
|
+
```typescript
|
|
230
|
+
// middleware.ts
|
|
231
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
232
|
+
import { nanoid } from 'nanoid';
|
|
233
|
+
|
|
234
|
+
export async function middleware(request: NextRequest) {
|
|
235
|
+
const response = NextResponse.next();
|
|
236
|
+
|
|
237
|
+
// Example: Get userId from a JWT or API call (replace with your logic)
|
|
238
|
+
const jwt = request.cookies.get('jwt')?.value;
|
|
239
|
+
let userId: string | undefined;
|
|
240
|
+
if (jwt) {
|
|
241
|
+
userId = 'user@example.com'; // Stub: e.g., await decodeJwt(jwt).email
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
if (userId) {
|
|
245
|
+
response.cookies.set('user-id', userId, { maxAge: 60 * 60 * 24 * 7 });
|
|
246
|
+
} else {
|
|
247
|
+
const visitorId = request.cookies.get('visitor-id')?.value ?? nanoid();
|
|
248
|
+
response.cookies.set('visitor-id', visitorId, { maxAge: 60 * 60 * 24 * 365 });
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
return response;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
export const config = {
|
|
255
|
+
matcher: [
|
|
256
|
+
/*
|
|
257
|
+
* Match all request paths except for the ones starting with:
|
|
258
|
+
* - api (API routes)
|
|
259
|
+
* - _next/static (static files)
|
|
260
|
+
* - _next/image (image optimization files)
|
|
261
|
+
* - favicon.ico (favicon file)
|
|
262
|
+
*/
|
|
263
|
+
{
|
|
264
|
+
source: '/((?!api|_next/static|_next/image|favicon.ico).*)',
|
|
265
|
+
missing: [
|
|
266
|
+
{ type: 'header', key: 'next-router-prefetch' },
|
|
267
|
+
{ type: 'header', key: 'purpose', value: 'prefetch' },
|
|
268
|
+
],
|
|
269
|
+
},
|
|
270
|
+
],
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
## Next Steps
|
|
276
|
+
|
|
277
|
+
- Configure flags in the [FlagSync dashboard](https://www.flagsync.com/dashboard).
|
|
278
|
+
- Explore the [Docs](https://docs.flagsync.com/) for information on how to use FlagSync.
|
|
279
|
+
|
|
280
|
+
## Environment Variables
|
|
281
|
+
|
|
282
|
+
Required environment variables:
|
|
283
|
+
|
|
284
|
+
- `FLAGSYNC_SDK_KEY`: Your server-side FlagSync SDK key (required)
|
|
285
|
+
|
|
286
|
+
### License
|
|
287
|
+
|
|
288
|
+
[](https://www.apache.org/licenses/LICENSE-2.0)
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
SyncType: () => import_node_sdk2.SyncType,
|
|
24
|
+
createBoolFlagAdaptor: () => createBoolFlagAdaptor,
|
|
25
|
+
createFlagSyncAdapter: () => createFlagSyncAdapter,
|
|
26
|
+
createFlagSyncClient: () => createFlagSyncClient,
|
|
27
|
+
createIdentify: () => createIdentify,
|
|
28
|
+
createJsonFlagAdaptor: () => createJsonFlagAdaptor,
|
|
29
|
+
createNumberFlagAdaptor: () => createNumberFlagAdaptor,
|
|
30
|
+
createStringFlagAdaptor: () => createStringFlagAdaptor
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(index_exports);
|
|
33
|
+
var import_node_sdk2 = require("@flagsync/node-sdk");
|
|
34
|
+
|
|
35
|
+
// src/adapter.ts
|
|
36
|
+
var import_node_sdk = require("@flagsync/node-sdk");
|
|
37
|
+
function createFlagSyncClient(config) {
|
|
38
|
+
const instance = (0, import_node_sdk.FlagSyncFactory)({
|
|
39
|
+
...config,
|
|
40
|
+
metadata: {
|
|
41
|
+
sdkName: "@flagsync/nextjs-sdk",
|
|
42
|
+
sdkVersion: "0.2.6"
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
return instance.client();
|
|
46
|
+
}
|
|
47
|
+
function createFlagSyncAdapter(client) {
|
|
48
|
+
let isReady = false;
|
|
49
|
+
const ensureReady = async () => {
|
|
50
|
+
if (!isReady) {
|
|
51
|
+
await client.waitForReady();
|
|
52
|
+
isReady = true;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
return function flagSyncAdapter() {
|
|
56
|
+
return {
|
|
57
|
+
/**
|
|
58
|
+
* Evaluates a feature flag for a given context and returns its value.
|
|
59
|
+
*/
|
|
60
|
+
async decide({ key, entities }) {
|
|
61
|
+
await ensureReady();
|
|
62
|
+
const userContext = {
|
|
63
|
+
key: "anonymous",
|
|
64
|
+
...entities ?? {}
|
|
65
|
+
};
|
|
66
|
+
return client.flag(userContext, key);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
var createStringFlagAdaptor = (client) => createFlagSyncAdapter(client)();
|
|
72
|
+
var createBoolFlagAdaptor = (client) => createFlagSyncAdapter(client)();
|
|
73
|
+
var createNumberFlagAdaptor = (client) => createFlagSyncAdapter(client)();
|
|
74
|
+
var createJsonFlagAdaptor = (client) => createFlagSyncAdapter(client)();
|
|
75
|
+
|
|
76
|
+
// src/identify.ts
|
|
77
|
+
var createIdentify = (callback) => (params) => callback(params);
|
|
78
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
79
|
+
0 && (module.exports = {
|
|
80
|
+
SyncType,
|
|
81
|
+
createBoolFlagAdaptor,
|
|
82
|
+
createFlagSyncAdapter,
|
|
83
|
+
createFlagSyncClient,
|
|
84
|
+
createIdentify,
|
|
85
|
+
createJsonFlagAdaptor,
|
|
86
|
+
createNumberFlagAdaptor,
|
|
87
|
+
createStringFlagAdaptor
|
|
88
|
+
});
|
|
89
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/adapter.ts","../src/identify.ts"],"sourcesContent":["export type {\n CustomAttributes,\n CustomAttributeValue,\n FsConfig,\n FsClient,\n LogLevel,\n FsUserContext,\n} from '@flagsync/node-sdk';\n\nexport { SyncType } from '@flagsync/node-sdk';\n\nexport type { JsonObject } from './types';\n\nexport {\n createFlagSyncClient,\n createFlagSyncAdapter,\n createBoolFlagAdaptor,\n createStringFlagAdaptor,\n createJsonFlagAdaptor,\n createNumberFlagAdaptor,\n} from './adapter';\n\nexport { createIdentify } from './identify';\n","import {\n FlagSyncFactory,\n FsClient,\n type FsConfig,\n type FsUserContext,\n} from '@flagsync/node-sdk';\nimport type { Adapter } from '@vercel/flags';\n\nimport { JsonObject } from './types';\n\n/**\n * Creates a FlagSync client instance that can be used across multiple feature flags.\n * This function should be called once to create a singleton client for your application.\n */\nexport function createFlagSyncClient(config: FsConfig): FsClient {\n const instance = FlagSyncFactory({\n ...config,\n metadata: {\n sdkName: '@flagsync/nextjs-sdk',\n sdkVersion: '0.2.6',\n },\n });\n return instance.client();\n}\n\n/**\n * Creates an adapter factory that integrates FlagSync with Vercel's feature flag system.\n * This adapter handles the communication between your application and the FlagSync service.\n */\nexport function createFlagSyncAdapter(client: FsClient) {\n let isReady = false;\n\n /**\n * Ensures the FlagSync client is ready before making any flag decisions.\n * This is called internally before each flag evaluation.\n */\n const ensureReady = async () => {\n if (!isReady) {\n await client.waitForReady();\n isReady = true;\n }\n };\n\n /**\n * Creates a typed adapter instance for a specific flag value type.\n */\n return function flagSyncAdapter<ValueType>(): Adapter<\n ValueType,\n FsUserContext\n > {\n return {\n /**\n * Evaluates a feature flag for a given context and returns its value.\n */\n async decide({ key, entities }) {\n await ensureReady();\n\n const userContext: FsUserContext = {\n key: 'anonymous',\n ...(entities ?? {}),\n };\n\n return client.flag<ValueType>(userContext, key);\n },\n };\n };\n}\n\n/** Creates an adapter for string-typed flags */\nexport const createStringFlagAdaptor = (\n client: FsClient,\n): Adapter<string, FsUserContext> => createFlagSyncAdapter(client)<string>();\n\n/** Creates an adapter for boolean-typed flags */\nexport const createBoolFlagAdaptor = (\n client: FsClient,\n): Adapter<boolean, FsUserContext> => createFlagSyncAdapter(client)<boolean>();\n\n/** Creates an adapter for number-typed flags */\nexport const createNumberFlagAdaptor = (\n client: FsClient,\n): Adapter<number, FsUserContext> => createFlagSyncAdapter(client)<number>();\n\n/** Creates an adapter for JSON object-typed flags */\nexport const createJsonFlagAdaptor = (\n client: FsClient,\n): Adapter<JsonObject, FsUserContext> =>\n createFlagSyncAdapter(client)<JsonObject>();\n","import { FsUserContext } from '@flagsync/node-sdk';\nimport { Identify } from '@vercel/flags';\n\ntype ExtractParams<T> = T extends (params: infer P) => any ? P : never;\n\nexport const createIdentify =\n (\n callback: (\n params: ExtractParams<Identify<FsUserContext>>,\n ) => Promise<FsUserContext> | FsUserContext,\n ): Identify<FsUserContext> =>\n (params) =>\n callback(params);\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AASA,IAAAA,mBAAyB;;;ACTzB,sBAKO;AASA,SAAS,qBAAqB,QAA4B;AAC/D,QAAM,eAAW,iCAAgB;AAAA,IAC/B,GAAG;AAAA,IACH,UAAU;AAAA,MACR,SAAS;AAAA,MACT,YAAY;AAAA,IACd;AAAA,EACF,CAAC;AACD,SAAO,SAAS,OAAO;AACzB;AAMO,SAAS,sBAAsB,QAAkB;AACtD,MAAI,UAAU;AAMd,QAAM,cAAc,YAAY;AAC9B,QAAI,CAAC,SAAS;AACZ,YAAM,OAAO,aAAa;AAC1B,gBAAU;AAAA,IACZ;AAAA,EACF;AAKA,SAAO,SAAS,kBAGd;AACA,WAAO;AAAA;AAAA;AAAA;AAAA,MAIL,MAAM,OAAO,EAAE,KAAK,SAAS,GAAG;AAC9B,cAAM,YAAY;AAElB,cAAM,cAA6B;AAAA,UACjC,KAAK;AAAA,UACL,GAAI,YAAY,CAAC;AAAA,QACnB;AAEA,eAAO,OAAO,KAAgB,aAAa,GAAG;AAAA,MAChD;AAAA,IACF;AAAA,EACF;AACF;AAGO,IAAM,0BAA0B,CACrC,WACmC,sBAAsB,MAAM,EAAU;AAGpE,IAAM,wBAAwB,CACnC,WACoC,sBAAsB,MAAM,EAAW;AAGtE,IAAM,0BAA0B,CACrC,WACmC,sBAAsB,MAAM,EAAU;AAGpE,IAAM,wBAAwB,CACnC,WAEA,sBAAsB,MAAM,EAAc;;;AClFrC,IAAM,iBACX,CACE,aAIF,CAAC,WACC,SAAS,MAAM;","names":["import_node_sdk"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { FsConfig, FsClient, FsUserContext } from '@flagsync/node-sdk';
|
|
2
|
+
export { CustomAttributeValue, CustomAttributes, FsClient, FsConfig, FsUserContext, LogLevel, SyncType } from '@flagsync/node-sdk';
|
|
3
|
+
import { Adapter, Identify } from '@vercel/flags';
|
|
4
|
+
|
|
5
|
+
type JsonPrimitive = string | number | boolean | null;
|
|
6
|
+
type JsonValue = JsonPrimitive | JsonObject | JsonArray;
|
|
7
|
+
type JsonArray = Array<JsonValue>;
|
|
8
|
+
interface JsonObject {
|
|
9
|
+
[key: string]: JsonValue;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Creates a FlagSync client instance that can be used across multiple feature flags.
|
|
14
|
+
* This function should be called once to create a singleton client for your application.
|
|
15
|
+
*/
|
|
16
|
+
declare function createFlagSyncClient(config: FsConfig): FsClient;
|
|
17
|
+
/**
|
|
18
|
+
* Creates an adapter factory that integrates FlagSync with Vercel's feature flag system.
|
|
19
|
+
* This adapter handles the communication between your application and the FlagSync service.
|
|
20
|
+
*/
|
|
21
|
+
declare function createFlagSyncAdapter(client: FsClient): <ValueType>() => Adapter<ValueType, FsUserContext>;
|
|
22
|
+
/** Creates an adapter for string-typed flags */
|
|
23
|
+
declare const createStringFlagAdaptor: (client: FsClient) => Adapter<string, FsUserContext>;
|
|
24
|
+
/** Creates an adapter for boolean-typed flags */
|
|
25
|
+
declare const createBoolFlagAdaptor: (client: FsClient) => Adapter<boolean, FsUserContext>;
|
|
26
|
+
/** Creates an adapter for number-typed flags */
|
|
27
|
+
declare const createNumberFlagAdaptor: (client: FsClient) => Adapter<number, FsUserContext>;
|
|
28
|
+
/** Creates an adapter for JSON object-typed flags */
|
|
29
|
+
declare const createJsonFlagAdaptor: (client: FsClient) => Adapter<JsonObject, FsUserContext>;
|
|
30
|
+
|
|
31
|
+
type ExtractParams<T> = T extends (params: infer P) => any ? P : never;
|
|
32
|
+
declare const createIdentify: (callback: (params: ExtractParams<Identify<FsUserContext>>) => Promise<FsUserContext> | FsUserContext) => Identify<FsUserContext>;
|
|
33
|
+
|
|
34
|
+
export { type JsonObject, createBoolFlagAdaptor, createFlagSyncAdapter, createFlagSyncClient, createIdentify, createJsonFlagAdaptor, createNumberFlagAdaptor, createStringFlagAdaptor };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { FsConfig, FsClient, FsUserContext } from '@flagsync/node-sdk';
|
|
2
|
+
export { CustomAttributeValue, CustomAttributes, FsClient, FsConfig, FsUserContext, LogLevel, SyncType } from '@flagsync/node-sdk';
|
|
3
|
+
import { Adapter, Identify } from '@vercel/flags';
|
|
4
|
+
|
|
5
|
+
type JsonPrimitive = string | number | boolean | null;
|
|
6
|
+
type JsonValue = JsonPrimitive | JsonObject | JsonArray;
|
|
7
|
+
type JsonArray = Array<JsonValue>;
|
|
8
|
+
interface JsonObject {
|
|
9
|
+
[key: string]: JsonValue;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Creates a FlagSync client instance that can be used across multiple feature flags.
|
|
14
|
+
* This function should be called once to create a singleton client for your application.
|
|
15
|
+
*/
|
|
16
|
+
declare function createFlagSyncClient(config: FsConfig): FsClient;
|
|
17
|
+
/**
|
|
18
|
+
* Creates an adapter factory that integrates FlagSync with Vercel's feature flag system.
|
|
19
|
+
* This adapter handles the communication between your application and the FlagSync service.
|
|
20
|
+
*/
|
|
21
|
+
declare function createFlagSyncAdapter(client: FsClient): <ValueType>() => Adapter<ValueType, FsUserContext>;
|
|
22
|
+
/** Creates an adapter for string-typed flags */
|
|
23
|
+
declare const createStringFlagAdaptor: (client: FsClient) => Adapter<string, FsUserContext>;
|
|
24
|
+
/** Creates an adapter for boolean-typed flags */
|
|
25
|
+
declare const createBoolFlagAdaptor: (client: FsClient) => Adapter<boolean, FsUserContext>;
|
|
26
|
+
/** Creates an adapter for number-typed flags */
|
|
27
|
+
declare const createNumberFlagAdaptor: (client: FsClient) => Adapter<number, FsUserContext>;
|
|
28
|
+
/** Creates an adapter for JSON object-typed flags */
|
|
29
|
+
declare const createJsonFlagAdaptor: (client: FsClient) => Adapter<JsonObject, FsUserContext>;
|
|
30
|
+
|
|
31
|
+
type ExtractParams<T> = T extends (params: infer P) => any ? P : never;
|
|
32
|
+
declare const createIdentify: (callback: (params: ExtractParams<Identify<FsUserContext>>) => Promise<FsUserContext> | FsUserContext) => Identify<FsUserContext>;
|
|
33
|
+
|
|
34
|
+
export { type JsonObject, createBoolFlagAdaptor, createFlagSyncAdapter, createFlagSyncClient, createIdentify, createJsonFlagAdaptor, createNumberFlagAdaptor, createStringFlagAdaptor };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { SyncType } from "@flagsync/node-sdk";
|
|
3
|
+
|
|
4
|
+
// src/adapter.ts
|
|
5
|
+
import {
|
|
6
|
+
FlagSyncFactory
|
|
7
|
+
} from "@flagsync/node-sdk";
|
|
8
|
+
function createFlagSyncClient(config) {
|
|
9
|
+
const instance = FlagSyncFactory({
|
|
10
|
+
...config,
|
|
11
|
+
metadata: {
|
|
12
|
+
sdkName: "@flagsync/nextjs-sdk",
|
|
13
|
+
sdkVersion: "0.2.6"
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
return instance.client();
|
|
17
|
+
}
|
|
18
|
+
function createFlagSyncAdapter(client) {
|
|
19
|
+
let isReady = false;
|
|
20
|
+
const ensureReady = async () => {
|
|
21
|
+
if (!isReady) {
|
|
22
|
+
await client.waitForReady();
|
|
23
|
+
isReady = true;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
return function flagSyncAdapter() {
|
|
27
|
+
return {
|
|
28
|
+
/**
|
|
29
|
+
* Evaluates a feature flag for a given context and returns its value.
|
|
30
|
+
*/
|
|
31
|
+
async decide({ key, entities }) {
|
|
32
|
+
await ensureReady();
|
|
33
|
+
const userContext = {
|
|
34
|
+
key: "anonymous",
|
|
35
|
+
...entities ?? {}
|
|
36
|
+
};
|
|
37
|
+
return client.flag(userContext, key);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
var createStringFlagAdaptor = (client) => createFlagSyncAdapter(client)();
|
|
43
|
+
var createBoolFlagAdaptor = (client) => createFlagSyncAdapter(client)();
|
|
44
|
+
var createNumberFlagAdaptor = (client) => createFlagSyncAdapter(client)();
|
|
45
|
+
var createJsonFlagAdaptor = (client) => createFlagSyncAdapter(client)();
|
|
46
|
+
|
|
47
|
+
// src/identify.ts
|
|
48
|
+
var createIdentify = (callback) => (params) => callback(params);
|
|
49
|
+
export {
|
|
50
|
+
SyncType,
|
|
51
|
+
createBoolFlagAdaptor,
|
|
52
|
+
createFlagSyncAdapter,
|
|
53
|
+
createFlagSyncClient,
|
|
54
|
+
createIdentify,
|
|
55
|
+
createJsonFlagAdaptor,
|
|
56
|
+
createNumberFlagAdaptor,
|
|
57
|
+
createStringFlagAdaptor
|
|
58
|
+
};
|
|
59
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/adapter.ts","../src/identify.ts"],"sourcesContent":["export type {\n CustomAttributes,\n CustomAttributeValue,\n FsConfig,\n FsClient,\n LogLevel,\n FsUserContext,\n} from '@flagsync/node-sdk';\n\nexport { SyncType } from '@flagsync/node-sdk';\n\nexport type { JsonObject } from './types';\n\nexport {\n createFlagSyncClient,\n createFlagSyncAdapter,\n createBoolFlagAdaptor,\n createStringFlagAdaptor,\n createJsonFlagAdaptor,\n createNumberFlagAdaptor,\n} from './adapter';\n\nexport { createIdentify } from './identify';\n","import {\n FlagSyncFactory,\n FsClient,\n type FsConfig,\n type FsUserContext,\n} from '@flagsync/node-sdk';\nimport type { Adapter } from '@vercel/flags';\n\nimport { JsonObject } from './types';\n\n/**\n * Creates a FlagSync client instance that can be used across multiple feature flags.\n * This function should be called once to create a singleton client for your application.\n */\nexport function createFlagSyncClient(config: FsConfig): FsClient {\n const instance = FlagSyncFactory({\n ...config,\n metadata: {\n sdkName: '@flagsync/nextjs-sdk',\n sdkVersion: '0.2.6',\n },\n });\n return instance.client();\n}\n\n/**\n * Creates an adapter factory that integrates FlagSync with Vercel's feature flag system.\n * This adapter handles the communication between your application and the FlagSync service.\n */\nexport function createFlagSyncAdapter(client: FsClient) {\n let isReady = false;\n\n /**\n * Ensures the FlagSync client is ready before making any flag decisions.\n * This is called internally before each flag evaluation.\n */\n const ensureReady = async () => {\n if (!isReady) {\n await client.waitForReady();\n isReady = true;\n }\n };\n\n /**\n * Creates a typed adapter instance for a specific flag value type.\n */\n return function flagSyncAdapter<ValueType>(): Adapter<\n ValueType,\n FsUserContext\n > {\n return {\n /**\n * Evaluates a feature flag for a given context and returns its value.\n */\n async decide({ key, entities }) {\n await ensureReady();\n\n const userContext: FsUserContext = {\n key: 'anonymous',\n ...(entities ?? {}),\n };\n\n return client.flag<ValueType>(userContext, key);\n },\n };\n };\n}\n\n/** Creates an adapter for string-typed flags */\nexport const createStringFlagAdaptor = (\n client: FsClient,\n): Adapter<string, FsUserContext> => createFlagSyncAdapter(client)<string>();\n\n/** Creates an adapter for boolean-typed flags */\nexport const createBoolFlagAdaptor = (\n client: FsClient,\n): Adapter<boolean, FsUserContext> => createFlagSyncAdapter(client)<boolean>();\n\n/** Creates an adapter for number-typed flags */\nexport const createNumberFlagAdaptor = (\n client: FsClient,\n): Adapter<number, FsUserContext> => createFlagSyncAdapter(client)<number>();\n\n/** Creates an adapter for JSON object-typed flags */\nexport const createJsonFlagAdaptor = (\n client: FsClient,\n): Adapter<JsonObject, FsUserContext> =>\n createFlagSyncAdapter(client)<JsonObject>();\n","import { FsUserContext } from '@flagsync/node-sdk';\nimport { Identify } from '@vercel/flags';\n\ntype ExtractParams<T> = T extends (params: infer P) => any ? P : never;\n\nexport const createIdentify =\n (\n callback: (\n params: ExtractParams<Identify<FsUserContext>>,\n ) => Promise<FsUserContext> | FsUserContext,\n ): Identify<FsUserContext> =>\n (params) =>\n callback(params);\n"],"mappings":";AASA,SAAS,gBAAgB;;;ACTzB;AAAA,EACE;AAAA,OAIK;AASA,SAAS,qBAAqB,QAA4B;AAC/D,QAAM,WAAW,gBAAgB;AAAA,IAC/B,GAAG;AAAA,IACH,UAAU;AAAA,MACR,SAAS;AAAA,MACT,YAAY;AAAA,IACd;AAAA,EACF,CAAC;AACD,SAAO,SAAS,OAAO;AACzB;AAMO,SAAS,sBAAsB,QAAkB;AACtD,MAAI,UAAU;AAMd,QAAM,cAAc,YAAY;AAC9B,QAAI,CAAC,SAAS;AACZ,YAAM,OAAO,aAAa;AAC1B,gBAAU;AAAA,IACZ;AAAA,EACF;AAKA,SAAO,SAAS,kBAGd;AACA,WAAO;AAAA;AAAA;AAAA;AAAA,MAIL,MAAM,OAAO,EAAE,KAAK,SAAS,GAAG;AAC9B,cAAM,YAAY;AAElB,cAAM,cAA6B;AAAA,UACjC,KAAK;AAAA,UACL,GAAI,YAAY,CAAC;AAAA,QACnB;AAEA,eAAO,OAAO,KAAgB,aAAa,GAAG;AAAA,MAChD;AAAA,IACF;AAAA,EACF;AACF;AAGO,IAAM,0BAA0B,CACrC,WACmC,sBAAsB,MAAM,EAAU;AAGpE,IAAM,wBAAwB,CACnC,WACoC,sBAAsB,MAAM,EAAW;AAGtE,IAAM,0BAA0B,CACrC,WACmC,sBAAsB,MAAM,EAAU;AAGpE,IAAM,wBAAwB,CACnC,WAEA,sBAAsB,MAAM,EAAc;;;AClFrC,IAAM,iBACX,CACE,aAIF,CAAC,WACC,SAAS,MAAM;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@flagsync/nextjs-sdk",
|
|
3
|
+
"version": "0.2.6",
|
|
4
|
+
"description": "FlagSync adapter for Vercel's Flags SDK",
|
|
5
|
+
"author": "Mike Chabot",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/flagsync/nextjs-sdk"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://www.flagsync.com",
|
|
12
|
+
"type": "module",
|
|
13
|
+
"types": "dist/index.d.ts",
|
|
14
|
+
"main": "dist/index.cjs",
|
|
15
|
+
"module": "dist/index.js",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"import": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"default": "./dist/index.js"
|
|
21
|
+
},
|
|
22
|
+
"require": {
|
|
23
|
+
"types": "./dist/index.d.cts",
|
|
24
|
+
"default": "./dist/index.cjs"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"./package.json": "./package.json"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"dist",
|
|
31
|
+
"README.md",
|
|
32
|
+
"package.json"
|
|
33
|
+
],
|
|
34
|
+
"engines": {
|
|
35
|
+
"node": ">=16",
|
|
36
|
+
"npm": ">=6"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"@vercel/flags": "^3.1.1"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@flagsync/node-sdk": "^0.2.1"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@trivago/prettier-plugin-sort-imports": "^5.2.2",
|
|
46
|
+
"@types/node": "^20.14.2",
|
|
47
|
+
"@types/semver": "^7.5.8",
|
|
48
|
+
"@typescript-eslint/eslint-plugin": "^8.26.1",
|
|
49
|
+
"@typescript-eslint/parser": "^8.26.1",
|
|
50
|
+
"@vercel/flags": "^3.1.1",
|
|
51
|
+
"eslint": "^9.22.0",
|
|
52
|
+
"eslint-config-prettier": "^10.1.1",
|
|
53
|
+
"eslint-plugin-prettier": "^5.2.3",
|
|
54
|
+
"prettier": "^3.5.3",
|
|
55
|
+
"replace-in-file": "^7.2.0",
|
|
56
|
+
"semver": "^7.7.1",
|
|
57
|
+
"tsup": "^8.4.0",
|
|
58
|
+
"typescript": "^5.8.2",
|
|
59
|
+
"vite": "^6.2.1",
|
|
60
|
+
"vitest": "^3.0.8"
|
|
61
|
+
},
|
|
62
|
+
"scripts": {
|
|
63
|
+
"build": "tsup",
|
|
64
|
+
"format": "prettier --write \"src/**/*.ts\"",
|
|
65
|
+
"lint": "eslint \"src/**/*.ts\" --fix",
|
|
66
|
+
"test": "vitest"
|
|
67
|
+
}
|
|
68
|
+
}
|