@crossdelta/infrastructure 0.1.40 → 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 +42 -11
- 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
|
|
@@ -91,19 +90,20 @@ const config: ServiceConfig = {
|
|
|
91
90
|
}
|
|
92
91
|
```
|
|
93
92
|
|
|
94
|
-
###
|
|
93
|
+
### Internal Service with Custom URL (e.g., NATS)
|
|
95
94
|
|
|
96
95
|
```typescript
|
|
97
96
|
const config: ServiceConfig = {
|
|
98
97
|
name: 'nats',
|
|
99
|
-
internalPorts: [
|
|
98
|
+
internalPorts: [4222, 8222],
|
|
99
|
+
internalUrl: 'nats://nats:4222',
|
|
100
100
|
instanceSizeSlug: 'apps-s-1vcpu-1gb',
|
|
101
101
|
}
|
|
102
102
|
```
|
|
103
103
|
|
|
104
104
|
## CLI: generate-env
|
|
105
105
|
|
|
106
|
-
Generate `.env.local`
|
|
106
|
+
Generate `.env.local` and service-specific `env.ts` files for local development.
|
|
107
107
|
|
|
108
108
|
### Setup
|
|
109
109
|
|
|
@@ -112,16 +112,12 @@ Add a script to your workspace `package.json`:
|
|
|
112
112
|
```json
|
|
113
113
|
{
|
|
114
114
|
"scripts": {
|
|
115
|
-
"generate-env": "
|
|
115
|
+
"generate-env": "turbo run generate-env --ui=stream"
|
|
116
116
|
}
|
|
117
117
|
}
|
|
118
118
|
```
|
|
119
119
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
```bash
|
|
123
|
-
npm run generate-env
|
|
124
|
-
```
|
|
120
|
+
Or use `pf dev` which runs `generate-env` automatically before starting services.
|
|
125
121
|
|
|
126
122
|
### What it does
|
|
127
123
|
|
|
@@ -129,7 +125,8 @@ npm run generate-env
|
|
|
129
125
|
2. Discovers services from `infra/services/*.ts`
|
|
130
126
|
3. Generates `SERVICE_URL` and `SERVICE_PORT` env vars for localhost
|
|
131
127
|
4. Writes `.env.local` to your workspace root
|
|
132
|
-
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)
|
|
133
130
|
|
|
134
131
|
### Example output
|
|
135
132
|
|
|
@@ -144,8 +141,35 @@ API_GATEWAY_PORT=4000
|
|
|
144
141
|
STOREFRONT_PORT=3000
|
|
145
142
|
```
|
|
146
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
|
+
|
|
147
169
|
## API
|
|
148
170
|
|
|
171
|
+
### Main Export (`@crossdelta/infrastructure`)
|
|
172
|
+
|
|
149
173
|
| Function | Description |
|
|
150
174
|
|----------|-------------|
|
|
151
175
|
| `discoverServices(dir)` | Auto-discover service configs from directory |
|
|
@@ -155,6 +179,13 @@ STOREFRONT_PORT=3000
|
|
|
155
179
|
| `buildServicePortEnvs(configs)` | Create SERVICE_NAME_PORT env vars |
|
|
156
180
|
| `buildLocalUrls(configs)` | Generate localhost URLs for local dev |
|
|
157
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
|
+
|
|
158
189
|
## License
|
|
159
190
|
|
|
160
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;
|