@@ -166,6 +166,51 @@ export function client(...options)
166166 return new Client ( ...deepClone ( options ) )
167167}
168168
169+ /**
170+ * Metro API Client, extends Client
171+ * @param base ClientOptions|URL|String
172+ * @param methods {name:function,...} list of API methods to expose
173+ * This class extends the metro client to allow you to add your own
174+ * api client methods. Methods are bound to this API object.
175+ * All default client methods (get/post/put/etc.) still work, unless
176+ * overridden. If a response object has a data part, that will be
177+ * returned by the api client methods, instead of the normal response
178+ */
179+ export class API extends Client
180+ {
181+ constructor ( base , methods )
182+ {
183+ const getdatamw = async ( req , next ) => {
184+ let res = await next ( req )
185+ if ( res . ok && res . data ) {
186+ return res . data
187+ } else {
188+ return res
189+ }
190+ }
191+ if ( base instanceof Client ) {
192+ super ( base . clientOptions , getdatamw )
193+ } else {
194+ super ( base , getdatamw )
195+ }
196+ for ( const methodName in methods ) {
197+ if ( typeof methods [ methodName ] == 'function' ) {
198+ this [ methodName ] = methods [ methodName ] . bind ( this )
199+ }
200+ }
201+ }
202+ }
203+
204+ /**
205+ * Returns a new Metro API object
206+ * @param {...ClientOptions|string|URL }
207+ * @return API
208+ */
209+ export function api ( ...options )
210+ {
211+ return new API ( ...deepClone ( options ) )
212+ }
213+
169214function appendHeaders ( r , headers )
170215{
171216 if ( ! Array . isArray ( headers ) ) {
0 commit comments