@crossdelta/infrastructure 0.1.41 → 0.2.1
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 +39 -9
- package/dist/env.d.ts +0 -1
- package/dist/helpers/service-runtime.d.ts +4 -21
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -21,7 +21,6 @@ npm install @crossdelta/infrastructure
|
|
|
21
21
|
```
|
|
22
22
|
|
|
23
23
|
## Quick Start
|
|
24
|
-
|
|
25
24
|
### 1. Create a service config
|
|
26
25
|
|
|
27
26
|
```typescript
|
|
@@ -104,7 +103,7 @@ const config: ServiceConfig = {
|
|
|
104
103
|
|
|
105
104
|
## CLI: generate-env
|
|
106
105
|
|
|
107
|
-
Generate `.env.local`
|
|
106
|
+
Generate `.env.local` and service-specific `env.ts` files for local development.
|
|
108
107
|
|
|
109
108
|
### Setup
|
|
110
109
|
|
|
@@ -113,16 +112,12 @@ Add a script to your workspace `package.json`:
|
|
|
113
112
|
```json
|
|
114
113
|
{
|
|
115
114
|
"scripts": {
|
|
116
|
-
"generate-env": "
|
|
115
|
+
"generate-env": "turbo run generate-env --ui=stream"
|
|
117
116
|
}
|
|
118
117
|
}
|
|
119
118
|
```
|
|
120
119
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
```bash
|
|
124
|
-
npm run generate-env
|
|
125
|
-
```
|
|
120
|
+
Or use `pf dev` which runs `generate-env` automatically before starting services.
|
|
126
121
|
|
|
127
122
|
### What it does
|
|
128
123
|
|
|
@@ -130,7 +125,8 @@ npm run generate-env
|
|
|
130
125
|
2. Discovers services from `infra/services/*.ts`
|
|
131
126
|
3. Generates `SERVICE_URL` and `SERVICE_PORT` env vars for localhost
|
|
132
127
|
4. Writes `.env.local` to your workspace root
|
|
133
|
-
5. Generates
|
|
128
|
+
5. Generates `infra/env.ts` with type-safe `ServiceName` union type
|
|
129
|
+
6. Generates `services/*/src/env.ts` for each service (Docker-compatible)
|
|
134
130
|
|
|
135
131
|
### Example output
|
|
136
132
|
|
|
@@ -145,8 +141,35 @@ API_GATEWAY_PORT=4000
|
|
|
145
141
|
STOREFRONT_PORT=3000
|
|
146
142
|
```
|
|
147
143
|
|
|
144
|
+
`services/orders/src/env.ts`:
|
|
145
|
+
```typescript
|
|
146
|
+
import { getServicePort, getServiceUrl } from '@crossdelta/infrastructure/env'
|
|
147
|
+
|
|
148
|
+
export const SERVICE_NAME = 'orders' as const
|
|
149
|
+
export const getServicePort = (defaultPort = 8080) => _getServicePort(SERVICE_NAME, defaultPort)
|
|
150
|
+
export const getServiceUrl = () => _getServiceUrl(SERVICE_NAME)
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## Runtime Helpers
|
|
154
|
+
|
|
155
|
+
Use `@crossdelta/infrastructure/env` in your services to read environment configuration:
|
|
156
|
+
|
|
157
|
+
```typescript
|
|
158
|
+
import { getServicePort, getServiceUrl } from '@crossdelta/infrastructure/env'
|
|
159
|
+
|
|
160
|
+
// Get port from ORDERS_PORT env var (fallback: 8080)
|
|
161
|
+
const port = getServicePort('orders', 8080)
|
|
162
|
+
|
|
163
|
+
// Get URL from ORDERS_URL env var
|
|
164
|
+
const url = getServiceUrl('orders')
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
These helpers work both locally (via `.env.local`) and in production (via DO App Platform env injection).
|
|
168
|
+
|
|
148
169
|
## API
|
|
149
170
|
|
|
171
|
+
### Main Export (`@crossdelta/infrastructure`)
|
|
172
|
+
|
|
150
173
|
| Function | Description |
|
|
151
174
|
|----------|-------------|
|
|
152
175
|
| `discoverServices(dir)` | Auto-discover service configs from directory |
|
|
@@ -156,6 +179,13 @@ STOREFRONT_PORT=3000
|
|
|
156
179
|
| `buildServicePortEnvs(configs)` | Create SERVICE_NAME_PORT env vars |
|
|
157
180
|
| `buildLocalUrls(configs)` | Generate localhost URLs for local dev |
|
|
158
181
|
|
|
182
|
+
### Env Export (`@crossdelta/infrastructure/env`)
|
|
183
|
+
|
|
184
|
+
| Function | Description |
|
|
185
|
+
|----------|-------------|
|
|
186
|
+
| `getServicePort(name, default)` | Read SERVICE_NAME_PORT from env |
|
|
187
|
+
| `getServiceUrl(name)` | Read SERVICE_NAME_URL from env |
|
|
188
|
+
|
|
159
189
|
## License
|
|
160
190
|
|
|
161
191
|
MIT
|
package/dist/env.d.ts
CHANGED
|
@@ -3,37 +3,20 @@
|
|
|
3
3
|
* These functions read from environment variables set by generate-env (local)
|
|
4
4
|
* or injected by DO App Platform (production).
|
|
5
5
|
*/
|
|
6
|
-
import type { ServiceName } from '../types';
|
|
7
6
|
/**
|
|
8
7
|
* Get the port for a service from environment variables.
|
|
9
|
-
* Reads from
|
|
8
|
+
* Reads from SERVICE_NAME_PORT (e.g., ORDERS_PORT, API_GATEWAY_PORT)
|
|
10
9
|
*
|
|
11
10
|
* @param serviceName - The service name (e.g., 'orders', 'api-gateway')
|
|
12
11
|
* @param defaultPort - Fallback port if env var is not set (default: 8080)
|
|
13
12
|
* @returns The configured port number
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* ```typescript
|
|
17
|
-
* import { getServicePort } from '@crossdelta/infrastructure/env'
|
|
18
|
-
*
|
|
19
|
-
* const port = getServicePort('orders', 3001)
|
|
20
|
-
* // Reads process.env.ORDERS_PORT, falls back to 3001
|
|
21
|
-
* ```
|
|
22
13
|
*/
|
|
23
|
-
export declare function getServicePort(serviceName:
|
|
14
|
+
export declare function getServicePort(serviceName: string, defaultPort?: number): number;
|
|
24
15
|
/**
|
|
25
16
|
* Get the URL for a service from environment variables.
|
|
26
|
-
* Reads from
|
|
17
|
+
* Reads from SERVICE_NAME_URL (e.g., ORDERS_URL, API_GATEWAY_URL)
|
|
27
18
|
*
|
|
28
19
|
* @param serviceName - The service name (e.g., 'orders', 'api-gateway')
|
|
29
20
|
* @returns The service URL or undefined if not set
|
|
30
|
-
*
|
|
31
|
-
* @example
|
|
32
|
-
* ```typescript
|
|
33
|
-
* import { getServiceUrl } from '@crossdelta/infrastructure/env'
|
|
34
|
-
*
|
|
35
|
-
* const ordersUrl = getServiceUrl('orders')
|
|
36
|
-
* // Returns process.env.ORDERS_URL
|
|
37
|
-
* ```
|
|
38
21
|
*/
|
|
39
|
-
export declare function getServiceUrl(serviceName:
|
|
22
|
+
export declare function getServiceUrl(serviceName: string): string | undefined;
|