11import api , { APIResponse , Route , assumeTrustedRoute , route } from "@forge/api" ;
22
33import { ApiArgs , ApiError , FLAGSMITH_API_V1 } from "../common" ;
4+ import { readApiKey } from "./storage" ;
45
56type Model = {
67 id : number ;
@@ -53,7 +54,7 @@ export type EnvironmentFeatureState = Model & {
5354const flagsmithApi = async (
5455 apiKey : string ,
5556 route : Route ,
56- { method = "GET" , headers, body, codes = [ ] , jsonResponse = true } : ApiArgs = { }
57+ { method = "GET" , headers, body, codes = [ ] , jsonResponse = true } : ApiArgs = { } ,
5758) : Promise < unknown > => {
5859 try {
5960 const url = `${ FLAGSMITH_API_V1 } ${ route . value } ` ;
@@ -84,13 +85,16 @@ const flagsmithApi = async (
8485const checkResponse = ( response : APIResponse , ...codes : number [ ] ) : void => {
8586 if ( ! response . ok && ! codes . includes ( response . status ) ) {
8687 console . warn ( response . status , response . statusText ) ;
87- throw new ApiError ( "Unexpected Flagsmith API response:" , response . status ) ;
88+ throw new ApiError (
89+ `Unexpected Flagsmith API response: ${ response . statusText } ` ,
90+ response . status ,
91+ ) ;
8892 }
8993} ;
9094
9195const unpaginate = async < TModel extends Model > (
9296 apiKey : string ,
93- data : PaginatedModels < TModel >
97+ data : PaginatedModels < TModel > ,
9498) : Promise < TModel [ ] > => {
9599 let pageData = data ;
96100 const results = pageData ?. results ?? [ ] ;
@@ -103,13 +107,14 @@ const unpaginate = async <TModel extends Model>(
103107} ;
104108
105109const checkApiKey = ( apiKey : string ) : void => {
106- if ( ! apiKey ) throw new ApiError ( "Flagsmith API Key not configured " , 400 ) ;
110+ if ( ! apiKey ) throw new ApiError ( "Flagsmith API Key not set " , 400 ) ;
107111} ;
108112
109- export type ReadOrganisations = ( args : { apiKey : string } ) => Promise < Organisation [ ] > ;
113+ export type ReadOrganisations = ( args : { apiKey ? : string } ) => Promise < Organisation [ ] > ;
110114
111- /** Read Flagsmith Organisations for given API Key */
112- export const readOrganisations : ReadOrganisations = async ( { apiKey } ) => {
115+ /** Read Flagsmith Organisations for stored/given API Key */
116+ export const readOrganisations : ReadOrganisations = async ( { apiKey : givenApiKey } ) => {
117+ const apiKey = givenApiKey ?? ( await readApiKey ( ) ) ;
113118 checkApiKey ( apiKey ) ;
114119 const path = route `/organisations/` ;
115120 const data = ( await flagsmithApi ( apiKey , path ) ) as PaginatedModels < Organisation > ;
@@ -118,15 +123,13 @@ export const readOrganisations: ReadOrganisations = async ({ apiKey }) => {
118123 return results ;
119124} ;
120125
121- export type ReadProjects = ( args : {
122- apiKey : string ;
123- organisationId : string | undefined ;
124- } ) => Promise < Project [ ] > ;
126+ export type ReadProjects = ( args : { organisationId : string | undefined } ) => Promise < Project [ ] > ;
125127
126- /** Read Flagsmith Projects for given API Key and Organisation ID */
127- export const readProjects : ReadProjects = async ( { apiKey, organisationId } ) => {
128+ /** Read Flagsmith Projects for stored API Key and given Organisation ID */
129+ export const readProjects : ReadProjects = async ( { organisationId } ) => {
130+ const apiKey = await readApiKey ( ) ;
128131 checkApiKey ( apiKey ) ;
129- if ( ! organisationId ) throw new ApiError ( "Flagsmith organisation not configured " , 400 ) ;
132+ if ( ! organisationId ) throw new ApiError ( "Flagsmith organisation not connected " , 400 ) ;
130133 const path = route `/projects/?organisation=${ organisationId } ` ;
131134 const data = ( await flagsmithApi ( apiKey , path ) ) as Project [ ] ;
132135 // do not unpaginate as this API does not do pagination
@@ -135,31 +138,27 @@ export const readProjects: ReadProjects = async ({ apiKey, organisationId }) =>
135138 return results ;
136139} ;
137140
138- export type ReadEnvironments = ( args : {
139- apiKey : string ;
140- projectId : string | undefined ;
141- } ) => Promise < Environment [ ] > ;
141+ export type ReadEnvironments = ( args : { projectId : string | undefined } ) => Promise < Environment [ ] > ;
142142
143- /** Read Flagsmith Environments for given API Key and Project ID */
144- export const readEnvironments : ReadEnvironments = async ( { apiKey, projectId } ) => {
143+ /** Read Flagsmith Environments for stored API Key and given Project ID */
144+ export const readEnvironments : ReadEnvironments = async ( { projectId } ) => {
145+ const apiKey = await readApiKey ( ) ;
145146 checkApiKey ( apiKey ) ;
146- if ( ! projectId ) throw new ApiError ( "Flagsmith project not configured " , 400 ) ;
147+ if ( ! projectId ) throw new ApiError ( "Flagsmith project not connected " , 400 ) ;
147148 const path = route `/environments/?project=${ projectId } ` ;
148149 const data = ( await flagsmithApi ( apiKey , path ) ) as PaginatedModels < Environment > ;
149150 const results = await unpaginate ( apiKey , data ) ;
150151 if ( results . length === 0 ) throw new ApiError ( "Flagsmith project has no environments" , 404 ) ;
151152 return results ;
152153} ;
153154
154- export type ReadFeatures = ( args : {
155- apiKey : string ;
156- projectId : string | undefined ;
157- } ) => Promise < Feature [ ] > ;
155+ export type ReadFeatures = ( args : { projectId : string | undefined } ) => Promise < Feature [ ] > ;
158156
159- /** Read Flagsmith Features for given API Key and Project ID */
160- export const readFeatures : ReadFeatures = async ( { apiKey, projectId } ) => {
157+ /** Read Flagsmith Features for stored API Key and given Project ID */
158+ export const readFeatures : ReadFeatures = async ( { projectId } ) => {
159+ const apiKey = await readApiKey ( ) ;
161160 checkApiKey ( apiKey ) ;
162- if ( ! projectId ) throw new ApiError ( "Flagsmith project not configured " , 400 ) ;
161+ if ( ! projectId ) throw new ApiError ( "Flagsmith project not connected " , 400 ) ;
163162 const path = route `/projects/${ projectId } /features/` ;
164163 const data = ( await flagsmithApi ( apiKey , path ) ) as PaginatedModels < Feature > ;
165164 // ignore archived features
@@ -169,19 +168,18 @@ export const readFeatures: ReadFeatures = async ({ apiKey, projectId }) => {
169168} ;
170169
171170export type ReadEnvironmentFeatureState = ( args : {
172- apiKey : string ;
173171 envApiKey : string ;
174172 featureName : string ;
175173} ) => Promise < EnvironmentFeatureState > ;
176174
177- /** Read Flagsmith Feature State for given API Key, Environment API Key and Feature Name */
175+ /** Read Flagsmith Feature State for stored API Key and given Environment API Key and Feature Name */
178176export const readEnvironmentFeatureState : ReadEnvironmentFeatureState = async ( {
179- apiKey,
180177 envApiKey,
181178 featureName,
182179} ) => {
180+ const apiKey = await readApiKey ( ) ;
183181 checkApiKey ( apiKey ) ;
184- if ( ! envApiKey ) throw new ApiError ( "Flagsmith environment not configured " , 400 ) ;
182+ if ( ! envApiKey ) throw new ApiError ( "Flagsmith environment not connected " , 400 ) ;
185183 const path = route `/environments/${ envApiKey } /featurestates/?feature_name=${ featureName } ` ;
186184 const data = ( await flagsmithApi ( apiKey , path ) ) as PaginatedModels < EnvironmentFeatureState > ;
187185 const results = await unpaginate ( apiKey , data ) ;
0 commit comments