| 109 | const ORG_SELECTOR_HEADER = "x-executor-organization"; |
| 110 | |
| 111 | const postJson = (target: TargetShape, path: string, identity: Identity, body: unknown) => |
| 112 | Effect.promise(async () => { |
| 113 | const response = await fetch(new URL(path, target.baseUrl), { |
| 114 | method: "POST", |
| 115 | headers: { |
| 116 | // The identity's own headers (session cookie + org selector) exactly |
| 117 | // as its API client would send them — org-scoped endpoints fail |
| 118 | // closed without the selector. |
| 119 | ...(identity.headers ?? {}), |
| 120 | "content-type": "application/json", |
| 121 | origin: new URL(target.baseUrl).origin, |
| 122 | }, |
| 123 | body: JSON.stringify(body), |
| 124 | }); |
| 125 | if (!response.ok) { |
| 126 | throw new Error(`${path} failed (${response.status}): ${await response.text()}`); |
| 127 | } |
| 128 | return response; |
| 129 | }); |
| 130 | |
| 131 | const withRefreshedSession = ( |
| 132 | identity: Identity, |