@startsimpli/hooks 0.4.15 → 0.4.18

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@startsimpli/hooks",
3
- "version": "0.4.15",
3
+ "version": "0.4.18",
4
4
  "description": "Shared React hooks for StartSimpli apps",
5
5
  "main": "./src/index.ts",
6
6
  "types": "./src/index.ts",
@@ -23,7 +23,7 @@
23
23
  "@tanstack/react-query": ">=5.0.0",
24
24
  "react": ">=18.0.0",
25
25
  "react-native": ">=0.74.0",
26
- "@startsimpli/api": "0.5.22"
26
+ "@startsimpli/api": "0.5.27"
27
27
  },
28
28
  "peerDependenciesMeta": {
29
29
  "@tanstack/react-query": {
@@ -46,7 +46,7 @@
46
46
  "tsup": "^8.5.1",
47
47
  "typescript": "^6.0.3",
48
48
  "vitest": "^4.1.5",
49
- "@startsimpli/api": "0.5.22"
49
+ "@startsimpli/api": "0.5.27"
50
50
  },
51
51
  "scripts": {
52
52
  "build": "tsup",
@@ -2,7 +2,15 @@ import { describe, it, expect, vi } from 'vitest'
2
2
  import { renderHook, act, waitFor } from '@testing-library/react'
3
3
  import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
4
4
  import React from 'react'
5
- import { useCreateSecret, useDeleteSecret, useUpdateSecret } from '../useVault'
5
+ import {
6
+ useAssignEnvironmentProject,
7
+ useCreateSecret,
8
+ useDeleteProject,
9
+ useDeleteSecret,
10
+ useLinkSharedKey,
11
+ useRotateSharedKey,
12
+ useUpdateSecret,
13
+ } from '../useVault'
6
14
  import type { VaultApi } from '@startsimpli/api'
7
15
 
8
16
  /** startsim-drl: secret mutations must invalidate the env LIST (carries
@@ -105,3 +113,76 @@ describe('secret mutations also invalidate environment queries (startsim-drl)',
105
113
  )
106
114
  })
107
115
  })
116
+
117
+ describe('projects + global keys hooks (startsim-oftw, startsim-1m4g)', () => {
118
+ it('useAssignEnvironmentProject patches the env and refreshes env + project lists', async () => {
119
+ const qc = mkClient()
120
+ const seen = trackInvalidations(qc)
121
+ const api = fakeApi({ updateEnvironment: vi.fn().mockResolvedValue({ slug: 'e1', project: 'alpha' }) })
122
+ const { result } = renderHook(() => useAssignEnvironmentProject(api), { wrapper: wrap(qc) })
123
+
124
+ await act(async () => {
125
+ result.current.mutate({ slug: 'e1', project: 'alpha' })
126
+ })
127
+ await waitFor(() => expect(result.current.isSuccess).toBe(true))
128
+
129
+ expect(api.updateEnvironment).toHaveBeenCalledWith('e1', { project: 'alpha' })
130
+ expect(seen).toEqual(
131
+ expect.arrayContaining([['vault', 'environments'], ['vault', 'projects']]),
132
+ )
133
+ })
134
+
135
+ it('useDeleteProject refreshes both projects and environments (orphaned to Ungrouped)', async () => {
136
+ const qc = mkClient()
137
+ const seen = trackInvalidations(qc)
138
+ const api = fakeApi({ deleteProject: vi.fn().mockResolvedValue(undefined) })
139
+ const { result } = renderHook(() => useDeleteProject(api), { wrapper: wrap(qc) })
140
+
141
+ await act(async () => {
142
+ result.current.mutate('alpha')
143
+ })
144
+ await waitFor(() => expect(result.current.isSuccess).toBe(true))
145
+
146
+ expect(seen).toEqual(
147
+ expect.arrayContaining([['vault', 'projects'], ['vault', 'environments']]),
148
+ )
149
+ })
150
+
151
+ it('useRotateSharedKey invalidates shared-secrets, links and secrets so linked envs refresh', async () => {
152
+ const qc = mkClient()
153
+ const seen = trackInvalidations(qc)
154
+ const api = fakeApi({ rotateSharedSecret: vi.fn().mockResolvedValue({ id: 'ss1' }) })
155
+ const { result } = renderHook(() => useRotateSharedKey(api), { wrapper: wrap(qc) })
156
+
157
+ await act(async () => {
158
+ result.current.mutate({ id: 'ss1', value: 'sk-new' })
159
+ })
160
+ await waitFor(() => expect(result.current.isSuccess).toBe(true))
161
+
162
+ expect(api.rotateSharedSecret).toHaveBeenCalledWith('ss1', 'sk-new')
163
+ expect(seen).toEqual(
164
+ expect.arrayContaining([
165
+ ['vault', 'shared-secrets'],
166
+ ['vault', 'links'],
167
+ ['vault', 'secrets'],
168
+ ]),
169
+ )
170
+ })
171
+
172
+ it('useLinkSharedKey links and refreshes the env links + shared-secret counts', async () => {
173
+ const qc = mkClient()
174
+ const seen = trackInvalidations(qc)
175
+ const api = fakeApi({ linkSharedSecret: vi.fn().mockResolvedValue({ id: 'l1' }) })
176
+ const { result } = renderHook(() => useLinkSharedKey(api, 'prod'), { wrapper: wrap(qc) })
177
+
178
+ await act(async () => {
179
+ result.current.mutate('ss1')
180
+ })
181
+ await waitFor(() => expect(result.current.isSuccess).toBe(true))
182
+
183
+ expect(api.linkSharedSecret).toHaveBeenCalledWith('prod', 'ss1')
184
+ expect(seen).toEqual(
185
+ expect.arrayContaining([['vault', 'links', 'prod'], ['vault', 'shared-secrets']]),
186
+ )
187
+ })
188
+ })
package/src/index.ts CHANGED
@@ -70,7 +70,13 @@ export { useAsyncOptions } from './useAsyncOptions'
70
70
  export type { UseAsyncOptionsResult } from './useAsyncOptions'
71
71
 
72
72
  // Vault — environments, secrets, access keys, audit (startsim-d30.3.2)
73
+ // + projects + global/shared keys (startsim-oftw, startsim-1m4g)
73
74
  export {
75
+ useProjects,
76
+ useCreateProject,
77
+ useUpdateProject,
78
+ useDeleteProject,
79
+ useAssignEnvironmentProject,
74
80
  useEnvironments,
75
81
  useEnvironment,
76
82
  useCreateEnvironment,
@@ -81,6 +87,14 @@ export {
81
87
  useUpdateSecret,
82
88
  useDeleteSecret,
83
89
  useRevealSecret,
90
+ useSharedKeys,
91
+ useCreateSharedKey,
92
+ useUpdateSharedKey,
93
+ useDeleteSharedKey,
94
+ useRotateSharedKey,
95
+ useEnvironmentLinks,
96
+ useLinkSharedKey,
97
+ useUnlinkSharedKey,
84
98
  useAccessKeys,
85
99
  useCreateAccessKey,
86
100
  useDeleteAccessKey,
package/src/useVault.ts CHANGED
@@ -4,7 +4,9 @@ import type {
4
4
  VaultApi,
5
5
  VaultEnvironmentInput,
6
6
  VaultListParams,
7
+ VaultProjectInput,
7
8
  VaultSecretInput,
9
+ VaultSharedSecretInput,
8
10
  } from '@startsimpli/api'
9
11
 
10
12
  /** TanStack Query hooks over VaultApi (startsim-d30.3.2).
@@ -15,8 +17,11 @@ import type {
15
17
  */
16
18
 
17
19
  const ENVIRONMENTS_KEY = ['vault', 'environments'] as const
20
+ const PROJECTS_KEY = ['vault', 'projects'] as const
21
+ const SHARED_SECRETS_KEY = ['vault', 'shared-secrets'] as const
18
22
  const environmentKey = (slug: string) => ['vault', 'environment', slug]
19
23
  const secretsKey = (slug: string) => ['vault', 'secrets', slug]
24
+ const linksKey = (slug: string) => ['vault', 'links', slug]
20
25
  const accessKeysKey = (slug: string) => ['vault', 'access-keys', slug]
21
26
 
22
27
  /** Secret mutations alter the env's `secret_count`, so refresh the list +
@@ -28,6 +33,57 @@ function invalidateSecretQueries(qc: ReturnType<typeof useQueryClient>, slug: st
28
33
  qc.invalidateQueries({ queryKey: environmentKey(slug) })
29
34
  }
30
35
 
36
+ // --- Projects ---
37
+ export function useProjects(api: VaultApi, params?: VaultListParams) {
38
+ return useQuery({
39
+ queryKey: ['vault', 'projects', params],
40
+ queryFn: () => api.listProjects(params),
41
+ })
42
+ }
43
+
44
+ export function useCreateProject(api: VaultApi) {
45
+ const qc = useQueryClient()
46
+ return useMutation({
47
+ mutationFn: (data: VaultProjectInput) => api.createProject(data),
48
+ onSuccess: () => qc.invalidateQueries({ queryKey: PROJECTS_KEY }),
49
+ })
50
+ }
51
+
52
+ export function useUpdateProject(api: VaultApi) {
53
+ const qc = useQueryClient()
54
+ return useMutation({
55
+ mutationFn: ({ slug, data }: { slug: string; data: Partial<VaultProjectInput> }) =>
56
+ api.updateProject(slug, data),
57
+ onSuccess: () => qc.invalidateQueries({ queryKey: PROJECTS_KEY }),
58
+ })
59
+ }
60
+
61
+ export function useDeleteProject(api: VaultApi) {
62
+ const qc = useQueryClient()
63
+ return useMutation({
64
+ mutationFn: (slug: string) => api.deleteProject(slug),
65
+ onSuccess: () => {
66
+ // Deleting a project orphans its envs to Ungrouped → refresh both lists.
67
+ qc.invalidateQueries({ queryKey: PROJECTS_KEY })
68
+ qc.invalidateQueries({ queryKey: ENVIRONMENTS_KEY })
69
+ },
70
+ })
71
+ }
72
+
73
+ /** Assign (or clear) an environment's project. Thin wrapper over
74
+ * updateEnvironment so callers read as intent, not a generic patch. */
75
+ export function useAssignEnvironmentProject(api: VaultApi) {
76
+ const qc = useQueryClient()
77
+ return useMutation({
78
+ mutationFn: ({ slug, project }: { slug: string; project: string | null }) =>
79
+ api.updateEnvironment(slug, { project }),
80
+ onSuccess: () => {
81
+ qc.invalidateQueries({ queryKey: ENVIRONMENTS_KEY })
82
+ qc.invalidateQueries({ queryKey: PROJECTS_KEY })
83
+ },
84
+ })
85
+ }
86
+
31
87
  // --- Environments ---
32
88
  export function useEnvironments(api: VaultApi, params?: VaultListParams) {
33
89
  return useQuery({
@@ -132,6 +188,95 @@ export function useDeleteAccessKey(api: VaultApi, slug: string) {
132
188
  })
133
189
  }
134
190
 
191
+ // --- Shared secrets (global keys) ---
192
+ export function useSharedKeys(api: VaultApi, params?: VaultListParams) {
193
+ return useQuery({
194
+ queryKey: ['vault', 'shared-secrets', params],
195
+ queryFn: () => api.listSharedSecrets(params),
196
+ })
197
+ }
198
+
199
+ export function useCreateSharedKey(api: VaultApi) {
200
+ const qc = useQueryClient()
201
+ return useMutation({
202
+ mutationFn: (data: VaultSharedSecretInput) => api.createSharedSecret(data),
203
+ onSuccess: () => {
204
+ // Creating with a default_project can auto-link envs → refresh broadly.
205
+ qc.invalidateQueries({ queryKey: SHARED_SECRETS_KEY })
206
+ qc.invalidateQueries({ queryKey: ['vault', 'links'] })
207
+ },
208
+ })
209
+ }
210
+
211
+ export function useUpdateSharedKey(api: VaultApi) {
212
+ const qc = useQueryClient()
213
+ return useMutation({
214
+ mutationFn: ({ id, data }: { id: string; data: Partial<VaultSharedSecretInput> }) =>
215
+ api.updateSharedSecret(id, data),
216
+ onSuccess: () => {
217
+ qc.invalidateQueries({ queryKey: SHARED_SECRETS_KEY })
218
+ qc.invalidateQueries({ queryKey: ['vault', 'links'] })
219
+ },
220
+ })
221
+ }
222
+
223
+ export function useDeleteSharedKey(api: VaultApi) {
224
+ const qc = useQueryClient()
225
+ return useMutation({
226
+ mutationFn: (id: string) => api.deleteSharedSecret(id),
227
+ onSuccess: () => {
228
+ qc.invalidateQueries({ queryKey: SHARED_SECRETS_KEY })
229
+ qc.invalidateQueries({ queryKey: ['vault', 'links'] })
230
+ },
231
+ })
232
+ }
233
+
234
+ /** Rotate a shared key's single source value. Every linked env sees it on its
235
+ * next exchange — so invalidate shared-secret + all link/secret views. */
236
+ export function useRotateSharedKey(api: VaultApi) {
237
+ const qc = useQueryClient()
238
+ return useMutation({
239
+ mutationFn: ({ id, value }: { id: string; value: string }) =>
240
+ api.rotateSharedSecret(id, value),
241
+ onSuccess: () => {
242
+ qc.invalidateQueries({ queryKey: SHARED_SECRETS_KEY })
243
+ qc.invalidateQueries({ queryKey: ['vault', 'links'] })
244
+ qc.invalidateQueries({ queryKey: ['vault', 'secrets'] })
245
+ },
246
+ })
247
+ }
248
+
249
+ // --- Secret links (per-environment) ---
250
+ export function useEnvironmentLinks(api: VaultApi, slug: string, params?: VaultListParams) {
251
+ return useQuery({
252
+ queryKey: ['vault', 'links', slug, params],
253
+ queryFn: () => api.listLinks(slug, params),
254
+ enabled: !!slug,
255
+ })
256
+ }
257
+
258
+ export function useLinkSharedKey(api: VaultApi, slug: string) {
259
+ const qc = useQueryClient()
260
+ return useMutation({
261
+ mutationFn: (sharedSecretId: string) => api.linkSharedSecret(slug, sharedSecretId),
262
+ onSuccess: () => {
263
+ qc.invalidateQueries({ queryKey: linksKey(slug) })
264
+ qc.invalidateQueries({ queryKey: SHARED_SECRETS_KEY }) // linkedEnvCount changed
265
+ },
266
+ })
267
+ }
268
+
269
+ export function useUnlinkSharedKey(api: VaultApi, slug: string) {
270
+ const qc = useQueryClient()
271
+ return useMutation({
272
+ mutationFn: (linkId: string) => api.unlinkSharedSecret(slug, linkId),
273
+ onSuccess: () => {
274
+ qc.invalidateQueries({ queryKey: linksKey(slug) })
275
+ qc.invalidateQueries({ queryKey: SHARED_SECRETS_KEY })
276
+ },
277
+ })
278
+ }
279
+
135
280
  // --- Audit ---
136
281
  export function useAuditLog(api: VaultApi, slug: string, params?: VaultListParams) {
137
282
  return useQuery({