@good-food/utils 1.0.0

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/README.md ADDED
@@ -0,0 +1,121 @@
1
+ # @good-food/utils
2
+
3
+ Shared utilities library for Good Food microservices.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ bun add @good-food/utils
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ### Authentication Middleware
14
+
15
+ ```typescript
16
+ import { Elysia } from 'elysia';
17
+ import { authMiddleware, createAuthMiddleware, Role } from '@good-food/utils';
18
+
19
+ const app = new Elysia()
20
+ // Apply default auth middleware (just verifies JWT)
21
+ .use(authMiddleware)
22
+ .get('/protected', ({ user }) => {
23
+ return { message: `Hello ${user?.email}` };
24
+ });
25
+
26
+ // Or with role-based access control
27
+ const adminMiddleware = createAuthMiddleware({
28
+ allowedRoles: [Role.ADMIN]
29
+ });
30
+
31
+ const app = new Elysia()
32
+ .use(adminMiddleware)
33
+ .get('/admin', ({ user }) => {
34
+ return { message: 'Admin area' };
35
+ });
36
+ ```
37
+
38
+ ### Available Roles
39
+
40
+ - `Role.ADMIN`
41
+ - `Role.FRANCHISE_OWNER`
42
+ - `Role.STAFF`
43
+ - `Role.CUSTOMER`
44
+
45
+ ### Environment Variables
46
+
47
+ | Variable | Description |
48
+ |----------|-------------|
49
+ | `JWT_PUBLIC_KEY_BASE64` | Base64-encoded RS256 public key for JWT verification |
50
+
51
+ ## Development
52
+
53
+ ```bash
54
+ # Install dependencies
55
+ bun install
56
+
57
+ # Build the library
58
+ bun run build
59
+
60
+ # Run tests
61
+ bun test
62
+ ```
63
+
64
+ ## API Reference
65
+
66
+ ### `createAuthMiddleware(options?)`
67
+
68
+ Creates an authentication middleware with optional role-based access control.
69
+
70
+ **Options:**
71
+ - `allowedRoles?: Role[]` - Optional array of roles that are permitted access
72
+
73
+ ### `authMiddleware`
74
+
75
+ Default authentication middleware without role restrictions.
76
+
77
+ ### `extractRoles(user)`
78
+
79
+ Helper function to extract roles from the user payload.
80
+
81
+ ```typescript
82
+ import { extractRoles } from '@good-food/utils';
83
+
84
+ const roles = extractRoles(user);
85
+ // Returns: Role[]
86
+ ```
87
+
88
+ ## CI/CD
89
+
90
+ This package uses GitHub Actions for continuous integration and deployment.
91
+
92
+ ### Automatic Build
93
+ Every push to `main` and all pull requests trigger:
94
+ - Dependency installation
95
+ - Build verification
96
+ - Test execution
97
+
98
+ ### Publishing to npm
99
+ To publish a new version:
100
+
101
+ 1. Update the version in `package.json`
102
+ 2. Commit your changes
103
+ 3. Create and push a version tag:
104
+ ```bash
105
+ git tag v1.0.1
106
+ git push origin v1.0.1
107
+ ```
108
+
109
+ The GitHub Action will automatically build and publish to npm.
110
+
111
+ ### Required Secrets
112
+ Add the following secret to your GitHub repository:
113
+
114
+ | Secret | Description |
115
+ |--------|-------------|
116
+ | `NPM_TOKEN` | npm access token with publish permissions |
117
+
118
+ To create an npm token:
119
+ 1. Go to [npmjs.com](https://www.npmjs.com/) → Account Settings → Access Tokens
120
+ 2. Generate a new "Automation" token
121
+ 3. Add it as a repository secret in GitHub Settings → Secrets → Actions
@@ -0,0 +1,104 @@
1
+ import { Elysia } from 'elysia';
2
+ import { type JWTPayload } from 'jose';
3
+ export declare enum Role {
4
+ ADMIN = "ADMIN",
5
+ FRANCHISE_OWNER = "FRANCHISE_OWNER",
6
+ STAFF = "STAFF",
7
+ CUSTOMER = "CUSTOMER"
8
+ }
9
+ export interface UserPayload extends JWTPayload {
10
+ sub?: string;
11
+ email?: string;
12
+ role?: Array<{
13
+ role: {
14
+ role: string;
15
+ };
16
+ }>;
17
+ }
18
+ export interface AuthMiddlewareOptions {
19
+ allowedRoles?: Role[];
20
+ }
21
+ /**
22
+ * Creates an authentication middleware with optional role-based access control.
23
+ * @param options - Configuration options for the middleware
24
+ * @param options.allowedRoles - Optional array of roles that are permitted access
25
+ * @returns Elysia plugin with authentication
26
+ */
27
+ export declare const createAuthMiddleware: (options?: AuthMiddlewareOptions) => Elysia<"", {
28
+ decorator: {};
29
+ store: {};
30
+ derive: {
31
+ user: UserPayload | null;
32
+ };
33
+ resolve: {};
34
+ }, {
35
+ typebox: {};
36
+ error: {};
37
+ }, {
38
+ schema: {};
39
+ standaloneSchema: {};
40
+ macro: {};
41
+ macroFn: {};
42
+ parser: {};
43
+ response: {
44
+ 200: {
45
+ message: string;
46
+ };
47
+ };
48
+ }, {}, {
49
+ derive: {};
50
+ resolve: {};
51
+ schema: {};
52
+ standaloneSchema: {};
53
+ response: {};
54
+ }, {
55
+ derive: {};
56
+ resolve: {};
57
+ schema: {};
58
+ standaloneSchema: {};
59
+ response: {};
60
+ }>;
61
+ /**
62
+ * Default authentication middleware without role restrictions.
63
+ * Use this when you only need to verify that the user is authenticated.
64
+ */
65
+ export declare const authMiddleware: Elysia<"", {
66
+ decorator: {};
67
+ store: {};
68
+ derive: {
69
+ user: UserPayload | null;
70
+ };
71
+ resolve: {};
72
+ }, {
73
+ typebox: {};
74
+ error: {};
75
+ }, {
76
+ schema: {};
77
+ standaloneSchema: {};
78
+ macro: {};
79
+ macroFn: {};
80
+ parser: {};
81
+ response: {
82
+ 200: {
83
+ message: string;
84
+ };
85
+ };
86
+ }, {}, {
87
+ derive: {};
88
+ resolve: {};
89
+ schema: {};
90
+ standaloneSchema: {};
91
+ response: {};
92
+ }, {
93
+ derive: {};
94
+ resolve: {};
95
+ schema: {};
96
+ standaloneSchema: {};
97
+ response: {};
98
+ }>;
99
+ /**
100
+ * Helper to extract user roles from the JWT payload.
101
+ * @param user - The user payload from the JWT
102
+ * @returns Array of Role enums
103
+ */
104
+ export declare const extractRoles: (user: UserPayload | null) => Role[];
@@ -0,0 +1,5 @@
1
+ /**
2
+ * @good-food/utils
3
+ * Shared utilities library for Good Food microservices
4
+ */
5
+ export { Role, type UserPayload, type AuthMiddlewareOptions, createAuthMiddleware, authMiddleware, extractRoles, } from './auth.middleware';