@ahoo-wang/fetcher-decorator 0.5.0
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/LICENSE +201 -0
- package/README.md +274 -0
- package/README.zh-CN.md +272 -0
- package/dist/apiDecorator.d.ts +42 -0
- package/dist/apiDecorator.d.ts.map +1 -0
- package/dist/endpointDecorator.d.ts +58 -0
- package/dist/endpointDecorator.d.ts.map +1 -0
- package/dist/generated.d.ts +26 -0
- package/dist/generated.d.ts.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.es.js +1141 -0
- package/dist/index.umd.js +14 -0
- package/dist/parameterDecorator.d.ts +113 -0
- package/dist/parameterDecorator.d.ts.map +1 -0
- package/dist/reflection.d.ts +41 -0
- package/dist/reflection.d.ts.map +1 -0
- package/dist/requestExecutor.d.ts +103 -0
- package/dist/requestExecutor.d.ts.map +1 -0
- package/package.json +55 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
(function(p,P){typeof exports=="object"&&typeof module<"u"?P(exports,require("@ahoo-wang/fetcher")):typeof define=="function"&&define.amd?define(["exports","@ahoo-wang/fetcher"],P):(p=typeof globalThis<"u"?globalThis:p||self,P(p.FetcherDecorator={},p.Fetcher))})(this,(function(p,P){"use strict";var X=typeof globalThis<"u"?globalThis:typeof window<"u"?window:typeof global<"u"?global:typeof self<"u"?self:{},$={};/*! *****************************************************************************
|
|
2
|
+
Copyright (C) Microsoft. All rights reserved.
|
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
4
|
+
this file except in compliance with the License. You may obtain a copy of the
|
|
5
|
+
License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
|
|
7
|
+
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
8
|
+
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
|
|
9
|
+
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
|
|
10
|
+
MERCHANTABLITY OR NON-INFRINGEMENT.
|
|
11
|
+
|
|
12
|
+
See the Apache Version 2.0 License for specific language governing permissions
|
|
13
|
+
and limitations under the License.
|
|
14
|
+
***************************************************************************** */var K;function Ee(){if(K)return $;K=1;var a;return(function(f){(function(d){var l=typeof globalThis=="object"?globalThis:typeof X=="object"?X:typeof self=="object"?self:typeof this=="object"?this:D(),w=E(f);typeof l.Reflect<"u"&&(w=E(l.Reflect,w)),d(w,l),typeof l.Reflect>"u"&&(l.Reflect=f);function E(j,q){return function(I,x){Object.defineProperty(j,I,{configurable:!0,writable:!0,value:x}),q&&q(I,x)}}function g(){try{return Function("return this;")()}catch{}}function T(){try{return(0,eval)("(function() { return this; })()")}catch{}}function D(){return g()||T()}})(function(d,l){var w=Object.prototype.hasOwnProperty,E=typeof Symbol=="function",g=E&&typeof Symbol.toPrimitive<"u"?Symbol.toPrimitive:"@@toPrimitive",T=E&&typeof Symbol.iterator<"u"?Symbol.iterator:"@@iterator",D=typeof Object.create=="function",j={__proto__:[]}instanceof Array,q=!D&&!j,I={create:D?function(){return J(Object.create(null))}:j?function(){return J({__proto__:null})}:function(){return J({})},has:q?function(e,t){return w.call(e,t)}:function(e,t){return t in e},get:q?function(e,t){return w.call(e,t)?e[t]:void 0}:function(e,t){return e[t]}},x=Object.getPrototypeOf(Function),F=typeof Map=="function"&&typeof Map.prototype.entries=="function"?Map:yt(),B=typeof Set=="function"&&typeof Set.prototype.entries=="function"?Set:vt(),L=typeof WeakMap=="function"?WeakMap:pt(),C=E?Symbol.for("@reflect-metadata:registry"):void 0,W=dt(),ue=lt(W);function ze(e,t,n,r){if(h(n)){if(!pe(e))throw new TypeError;if(!we(t))throw new TypeError;return rt(e,t)}else{if(!pe(e))throw new TypeError;if(!b(t))throw new TypeError;if(!b(r)&&!h(r)&&!G(r))throw new TypeError;return G(r)&&(r=void 0),n=R(n),at(e,t,n,r)}}d("decorate",ze);function Qe(e,t){function n(r,c){if(!b(r))throw new TypeError;if(!h(c)&&!st(c))throw new TypeError;de(e,t,r,c)}return n}d("metadata",Qe);function Ze(e,t,n,r){if(!b(n))throw new TypeError;return h(r)||(r=R(r)),de(e,t,n,r)}d("defineMetadata",Ze);function Je(e,t,n){if(!b(t))throw new TypeError;return h(n)||(n=R(n)),fe(e,t,n)}d("hasMetadata",Je);function Xe(e,t,n){if(!b(t))throw new TypeError;return h(n)||(n=R(n)),z(e,t,n)}d("hasOwnMetadata",Xe);function $e(e,t,n){if(!b(t))throw new TypeError;return h(n)||(n=R(n)),se(e,t,n)}d("getMetadata",$e);function Ke(e,t,n){if(!b(t))throw new TypeError;return h(n)||(n=R(n)),ce(e,t,n)}d("getOwnMetadata",Ke);function et(e,t){if(!b(e))throw new TypeError;return h(t)||(t=R(t)),le(e,t)}d("getMetadataKeys",et);function tt(e,t){if(!b(e))throw new TypeError;return h(t)||(t=R(t)),he(e,t)}d("getOwnMetadataKeys",tt);function nt(e,t,n){if(!b(t))throw new TypeError;if(h(n)||(n=R(n)),!b(t))throw new TypeError;h(n)||(n=R(n));var r=N(t,n,!1);return h(r)?!1:r.OrdinaryDeleteMetadata(e,t,n)}d("deleteMetadata",nt);function rt(e,t){for(var n=e.length-1;n>=0;--n){var r=e[n],c=r(t);if(!h(c)&&!G(c)){if(!we(c))throw new TypeError;t=c}}return t}function at(e,t,n,r){for(var c=e.length-1;c>=0;--c){var _=e[c],O=_(t,n,r);if(!h(O)&&!G(O)){if(!b(O))throw new TypeError;r=O}}return r}function fe(e,t,n){var r=z(e,t,n);if(r)return!0;var c=Z(t);return G(c)?!1:fe(e,c,n)}function z(e,t,n){var r=N(t,n,!1);return h(r)?!1:ve(r.OrdinaryHasOwnMetadata(e,t,n))}function se(e,t,n){var r=z(e,t,n);if(r)return ce(e,t,n);var c=Z(t);if(!G(c))return se(e,c,n)}function ce(e,t,n){var r=N(t,n,!1);if(!h(r))return r.OrdinaryGetOwnMetadata(e,t,n)}function de(e,t,n,r){var c=N(n,r,!0);c.OrdinaryDefineOwnMetadata(e,t,n,r)}function le(e,t){var n=he(e,t),r=Z(e);if(r===null)return n;var c=le(r,t);if(c.length<=0)return n;if(n.length<=0)return c;for(var _=new B,O=[],y=0,i=n;y<i.length;y++){var o=i[y],u=_.has(o);u||(_.add(o),O.push(o))}for(var s=0,v=c;s<v.length;s++){var o=v[s],u=_.has(o);u||(_.add(o),O.push(o))}return O}function he(e,t){var n=N(e,t,!1);return n?n.OrdinaryOwnMetadataKeys(e,t):[]}function ye(e){if(e===null)return 1;switch(typeof e){case"undefined":return 0;case"boolean":return 2;case"string":return 3;case"symbol":return 4;case"number":return 5;case"object":return e===null?1:6;default:return 6}}function h(e){return e===void 0}function G(e){return e===null}function it(e){return typeof e=="symbol"}function b(e){return typeof e=="object"?e!==null:typeof e=="function"}function ot(e,t){switch(ye(e)){case 0:return e;case 1:return e;case 2:return e;case 3:return e;case 4:return e;case 5:return e}var n="string",r=Me(e,g);if(r!==void 0){var c=r.call(e,n);if(b(c))throw new TypeError;return c}return ut(e)}function ut(e,t){var n,r,c;{var _=e.toString;if(Y(_)){var r=_.call(e);if(!b(r))return r}var n=e.valueOf;if(Y(n)){var r=n.call(e);if(!b(r))return r}}throw new TypeError}function ve(e){return!!e}function ft(e){return""+e}function R(e){var t=ot(e);return it(t)?t:ft(t)}function pe(e){return Array.isArray?Array.isArray(e):e instanceof Object?e instanceof Array:Object.prototype.toString.call(e)==="[object Array]"}function Y(e){return typeof e=="function"}function we(e){return typeof e=="function"}function st(e){switch(ye(e)){case 3:return!0;case 4:return!0;default:return!1}}function Q(e,t){return e===t||e!==e&&t!==t}function Me(e,t){var n=e[t];if(n!=null){if(!Y(n))throw new TypeError;return n}}function _e(e){var t=Me(e,T);if(!Y(t))throw new TypeError;var n=t.call(e);if(!b(n))throw new TypeError;return n}function me(e){return e.value}function ge(e){var t=e.next();return t.done?!1:t}function be(e){var t=e.return;t&&t.call(e)}function Z(e){var t=Object.getPrototypeOf(e);if(typeof e!="function"||e===x||t!==x)return t;var n=e.prototype,r=n&&Object.getPrototypeOf(n);if(r==null||r===Object.prototype)return t;var c=r.constructor;return typeof c!="function"||c===e?t:c}function ct(){var e;!h(C)&&typeof l.Reflect<"u"&&!(C in l.Reflect)&&typeof l.Reflect.defineMetadata=="function"&&(e=ht(l.Reflect));var t,n,r,c=new L,_={registerProvider:O,getProvider:i,setProvider:u};return _;function O(s){if(!Object.isExtensible(_))throw new Error("Cannot add provider to a frozen registry.");switch(!0){case e===s:break;case h(t):t=s;break;case t===s:break;case h(n):n=s;break;case n===s:break;default:r===void 0&&(r=new B),r.add(s);break}}function y(s,v){if(!h(t)){if(t.isProviderFor(s,v))return t;if(!h(n)){if(n.isProviderFor(s,v))return t;if(!h(r))for(var M=_e(r);;){var m=ge(M);if(!m)return;var k=me(m);if(k.isProviderFor(s,v))return be(M),k}}}if(!h(e)&&e.isProviderFor(s,v))return e}function i(s,v){var M=c.get(s),m;return h(M)||(m=M.get(v)),h(m)&&(m=y(s,v),h(m)||(h(M)&&(M=new F,c.set(s,M)),M.set(v,m))),m}function o(s){if(h(s))throw new TypeError;return t===s||n===s||!h(r)&&r.has(s)}function u(s,v,M){if(!o(M))throw new Error("Metadata provider not registered.");var m=i(s,v);if(m!==M){if(!h(m))return!1;var k=c.get(s);h(k)&&(k=new F,c.set(s,k)),k.set(v,M)}return!0}}function dt(){var e;return!h(C)&&b(l.Reflect)&&Object.isExtensible(l.Reflect)&&(e=l.Reflect[C]),h(e)&&(e=ct()),!h(C)&&b(l.Reflect)&&Object.isExtensible(l.Reflect)&&Object.defineProperty(l.Reflect,C,{enumerable:!1,configurable:!1,writable:!1,value:e}),e}function lt(e){var t=new L,n={isProviderFor:function(o,u){var s=t.get(o);return h(s)?!1:s.has(u)},OrdinaryDefineOwnMetadata:O,OrdinaryHasOwnMetadata:c,OrdinaryGetOwnMetadata:_,OrdinaryOwnMetadataKeys:y,OrdinaryDeleteMetadata:i};return W.registerProvider(n),n;function r(o,u,s){var v=t.get(o),M=!1;if(h(v)){if(!s)return;v=new F,t.set(o,v),M=!0}var m=v.get(u);if(h(m)){if(!s)return;if(m=new F,v.set(u,m),!e.setProvider(o,u,n))throw v.delete(u),M&&t.delete(o),new Error("Wrong provider for target.")}return m}function c(o,u,s){var v=r(u,s,!1);return h(v)?!1:ve(v.has(o))}function _(o,u,s){var v=r(u,s,!1);if(!h(v))return v.get(o)}function O(o,u,s,v){var M=r(s,v,!0);M.set(o,u)}function y(o,u){var s=[],v=r(o,u,!1);if(h(v))return s;for(var M=v.keys(),m=_e(M),k=0;;){var Oe=ge(m);if(!Oe)return s.length=k,s;var wt=me(Oe);try{s[k]=wt}catch(Mt){try{be(m)}finally{throw Mt}}k++}}function i(o,u,s){var v=r(u,s,!1);if(h(v)||!v.delete(o))return!1;if(v.size===0){var M=t.get(u);h(M)||(M.delete(s),M.size===0&&t.delete(M))}return!0}}function ht(e){var t=e.defineMetadata,n=e.hasOwnMetadata,r=e.getOwnMetadata,c=e.getOwnMetadataKeys,_=e.deleteMetadata,O=new L,y={isProviderFor:function(i,o){var u=O.get(i);return!h(u)&&u.has(o)?!0:c(i,o).length?(h(u)&&(u=new B,O.set(i,u)),u.add(o),!0):!1},OrdinaryDefineOwnMetadata:t,OrdinaryHasOwnMetadata:n,OrdinaryGetOwnMetadata:r,OrdinaryOwnMetadataKeys:c,OrdinaryDeleteMetadata:_};return y}function N(e,t,n){var r=W.getProvider(e,t);if(!h(r))return r;if(n){if(W.setProvider(e,t,ue))return ue;throw new Error("Illegal state.")}}function yt(){var e={},t=[],n=(function(){function y(i,o,u){this._index=0,this._keys=i,this._values=o,this._selector=u}return y.prototype["@@iterator"]=function(){return this},y.prototype[T]=function(){return this},y.prototype.next=function(){var i=this._index;if(i>=0&&i<this._keys.length){var o=this._selector(this._keys[i],this._values[i]);return i+1>=this._keys.length?(this._index=-1,this._keys=t,this._values=t):this._index++,{value:o,done:!1}}return{value:void 0,done:!0}},y.prototype.throw=function(i){throw this._index>=0&&(this._index=-1,this._keys=t,this._values=t),i},y.prototype.return=function(i){return this._index>=0&&(this._index=-1,this._keys=t,this._values=t),{value:i,done:!0}},y})(),r=(function(){function y(){this._keys=[],this._values=[],this._cacheKey=e,this._cacheIndex=-2}return Object.defineProperty(y.prototype,"size",{get:function(){return this._keys.length},enumerable:!0,configurable:!0}),y.prototype.has=function(i){return this._find(i,!1)>=0},y.prototype.get=function(i){var o=this._find(i,!1);return o>=0?this._values[o]:void 0},y.prototype.set=function(i,o){var u=this._find(i,!0);return this._values[u]=o,this},y.prototype.delete=function(i){var o=this._find(i,!1);if(o>=0){for(var u=this._keys.length,s=o+1;s<u;s++)this._keys[s-1]=this._keys[s],this._values[s-1]=this._values[s];return this._keys.length--,this._values.length--,Q(i,this._cacheKey)&&(this._cacheKey=e,this._cacheIndex=-2),!0}return!1},y.prototype.clear=function(){this._keys.length=0,this._values.length=0,this._cacheKey=e,this._cacheIndex=-2},y.prototype.keys=function(){return new n(this._keys,this._values,c)},y.prototype.values=function(){return new n(this._keys,this._values,_)},y.prototype.entries=function(){return new n(this._keys,this._values,O)},y.prototype["@@iterator"]=function(){return this.entries()},y.prototype[T]=function(){return this.entries()},y.prototype._find=function(i,o){if(!Q(this._cacheKey,i)){this._cacheIndex=-1;for(var u=0;u<this._keys.length;u++)if(Q(this._keys[u],i)){this._cacheIndex=u;break}}return this._cacheIndex<0&&o&&(this._cacheIndex=this._keys.length,this._keys.push(i),this._values.push(void 0)),this._cacheIndex},y})();return r;function c(y,i){return y}function _(y,i){return i}function O(y,i){return[y,i]}}function vt(){var e=(function(){function t(){this._map=new F}return Object.defineProperty(t.prototype,"size",{get:function(){return this._map.size},enumerable:!0,configurable:!0}),t.prototype.has=function(n){return this._map.has(n)},t.prototype.add=function(n){return this._map.set(n,n),this},t.prototype.delete=function(n){return this._map.delete(n)},t.prototype.clear=function(){this._map.clear()},t.prototype.keys=function(){return this._map.keys()},t.prototype.values=function(){return this._map.keys()},t.prototype.entries=function(){return this._map.entries()},t.prototype["@@iterator"]=function(){return this.keys()},t.prototype[T]=function(){return this.keys()},t})();return e}function pt(){var e=16,t=I.create(),n=r();return(function(){function i(){this._key=r()}return i.prototype.has=function(o){var u=c(o,!1);return u!==void 0?I.has(u,this._key):!1},i.prototype.get=function(o){var u=c(o,!1);return u!==void 0?I.get(u,this._key):void 0},i.prototype.set=function(o,u){var s=c(o,!0);return s[this._key]=u,this},i.prototype.delete=function(o){var u=c(o,!1);return u!==void 0?delete u[this._key]:!1},i.prototype.clear=function(){this._key=r()},i})();function r(){var i;do i="@@WeakMap@@"+y();while(I.has(t,i));return t[i]=!0,i}function c(i,o){if(!w.call(i,n)){if(!o)return;Object.defineProperty(i,n,{value:I.create()})}return i[n]}function _(i,o){for(var u=0;u<o;++u)i[u]=Math.random()*255|0;return i}function O(i){if(typeof Uint8Array=="function"){var o=new Uint8Array(i);return typeof crypto<"u"?crypto.getRandomValues(o):typeof msCrypto<"u"?msCrypto.getRandomValues(o):_(o,i),o}return _(new Array(i),i)}function y(){var i=O(e);i[6]=i[6]&79|64,i[8]=i[8]&191|128;for(var o="",u=0;u<e;++u){var s=i[u];(u===4||u===6||u===8)&&(o+="-"),s<16&&(o+="0"),o+=s.toString(16).toLowerCase()}return o}}function J(e){return e.__=void 0,delete e.__,e}})})(a||(a={})),$}Ee();const V=Symbol("endpoint:metadata");function A(a,f="",d={}){return function(l,w){const E={method:a,path:f,...d};Reflect.defineMetadata(V,E,l,w)}}function Te(a="",f={}){return A(P.HttpMethod.GET,a,f)}function Pe(a="",f={}){return A(P.HttpMethod.POST,a,f)}function ke(a="",f={}){return A(P.HttpMethod.PUT,a,f)}function Re(a="",f={}){return A(P.HttpMethod.DELETE,a,f)}function Ae(a="",f={}){return A(P.HttpMethod.PATCH,a,f)}function Ie(a="",f={}){return A(P.HttpMethod.HEAD,a,f)}function Se(a="",f={}){return A(P.HttpMethod.OPTIONS,a,f)}function ee(a){if(typeof a!="function")throw new TypeError("Expected a function");try{const f=a.toString().trim(),d=je(f);return De(d)?Ce(d):[]}catch{return[]}}function te(a,f,d,l){if(l)return l;try{const w=a[f];if(w){const E=ee(w);if(d<E.length)return E[d]}}catch{}}function De(a){return a!=null&&a.trim()!==""}function je(a){if(a.startsWith("(")){const l=ne(a,0);return l===-1?"":a.substring(1,l)}const f=a.indexOf("(");if(f===-1)return"";const d=ne(a,f);return d===-1?"":a.substring(f+1,d)}function ne(a,f){let d=1;for(let l=f+1;l<a.length;l++){const w=a[l];if(w==="(")d++;else if(w===")"&&(d--,d===0))return l}return-1}function Ce(a){return a.split(",").map(Ge).filter(He).map(qe)}function Ge(a){return a.trim()}function He(a){return a.length>0}function qe(a){let f=xe(a);return f=Fe(f),f.trim()}function xe(a){const f=a.indexOf("=");return f!==-1?a.substring(0,f):a}function Fe(a){const f=a.indexOf(":");return f!==-1?a.substring(0,f):a}var S=(a=>(a.PATH="path",a.QUERY="query",a.HEADER="header",a.BODY="body",a))(S||{});const U=Symbol("parameter:metadata");function H(a,f=""){return function(d,l,w){const E=te(d,l,w,f),g=Reflect.getMetadata(U,d,l)||[];g.push({type:a,name:E,index:w}),Reflect.defineMetadata(U,g,d,l)}}function Ne(a=""){return H("path",a)}function Ue(a=""){return H("query",a)}function We(a=""){return H("header",a)}function Ye(){return H("body")}class re{constructor(f,d,l,w){this.name=f,this.api=d,this.endpoint=l,this.parameters=w}get fetcher(){const f=this.endpoint.fetcher||this.api.fetcher||P.defaultFetcherName;return P.fetcherRegistrar.requiredGet(f)}resolveRequest(f){const d={},l={},w={...this.api.headers,...this.endpoint.headers};let E=null;return this.parameters.forEach(g=>{const T=f[g.index];switch(g.type){case S.PATH:g.name?d[g.name]=T:d["param"+g.index]=T;break;case S.QUERY:g.name?l[g.name]=T:l["param"+g.index]=T;break;case S.HEADER:g.name&&T!==void 0&&(w[g.name]=String(T));break;case S.BODY:E=T;break}}),{pathParams:d,queryParams:l,headers:w,body:E,timeout:this.resolveTimeout()}}resolvePath(){const f=this.endpoint.basePath||this.api.basePath||"",d=this.endpoint.path||"";return P.combineURLs(f,d)}resolveTimeout(){return this.endpoint.timeout||this.api.timeout}}class ae{constructor(f){this.metadata=f}async execute(f){const d=this.metadata.resolvePath(),l=this.metadata.resolveRequest(f);return await this.metadata.fetcher.fetch(d,l)}}const ie=Symbol("api:metadata");function Ve(a,f,d){const l=a.prototype[f];if(f==="constructor"||typeof l!="function")return;const w=Reflect.getMetadata(V,a.prototype,f);if(!w)return;const E=Reflect.getMetadata(U,a.prototype,f)||[],g=new re(f,d,w,E),T=new ae(g);a.prototype[f]=function(...D){return T.execute(D)}}function Be(a="",f={}){return function(d){const l={basePath:a,...f};Reflect.defineMetadata(ie,l,d),Object.getOwnPropertyNames(d.prototype).forEach(w=>{Ve(d,w,l)})}}class oe extends Error{constructor(){super("Implementation will be generated automatically."),this.name="AutoGenerated"}}const Le=()=>new oe;p.API_METADATA_KEY=ie,p.AutoGenerated=oe,p.ENDPOINT_METADATA_KEY=V,p.FunctionMetadata=re,p.PARAMETER_METADATA_KEY=U,p.ParameterType=S,p.RequestExecutor=ae,p.api=Be,p.autoGeneratedError=Le,p.body=Ye,p.del=Re,p.endpoint=A,p.get=Te,p.getParameterName=te,p.getParameterNames=ee,p.head=Ie,p.header=We,p.options=Se,p.parameter=H,p.patch=Ae,p.path=Ne,p.post=Pe,p.put=ke,p.query=Ue,Object.defineProperty(p,Symbol.toStringTag,{value:"Module"})}));
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parameter types for decorator parameters
|
|
3
|
+
*
|
|
4
|
+
* This enum defines the different types of parameters that can be used
|
|
5
|
+
* in API method decorators to specify how arguments should be handled
|
|
6
|
+
* in the HTTP request.
|
|
7
|
+
*/
|
|
8
|
+
export declare enum ParameterType {
|
|
9
|
+
/**
|
|
10
|
+
* Path parameter that will be inserted into the URL path
|
|
11
|
+
*
|
|
12
|
+
* Path parameters are used to specify dynamic parts of the URL path.
|
|
13
|
+
* They are defined using curly braces in the endpoint path.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* @get('/users/{id}')
|
|
18
|
+
* getUser(@path('id') userId: string): Promise<Response>
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
PATH = "path",
|
|
22
|
+
/**
|
|
23
|
+
* Query parameter that will be appended to the URL query string
|
|
24
|
+
*
|
|
25
|
+
* Query parameters are used to pass non-hierarchical data to the server.
|
|
26
|
+
* They appear after the '?' in the URL.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* @get('/users')
|
|
31
|
+
* getUsers(@query('limit') limit: number): Promise<Response>
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
QUERY = "query",
|
|
35
|
+
/**
|
|
36
|
+
* Header parameter that will be added to the request headers
|
|
37
|
+
*
|
|
38
|
+
* Header parameters are used to pass metadata about the request,
|
|
39
|
+
* such as authentication tokens or content type information.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```typescript
|
|
43
|
+
* @get('/users')
|
|
44
|
+
* getUsers(@header('Authorization') token: string): Promise<Response>
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
HEADER = "header",
|
|
48
|
+
/**
|
|
49
|
+
* Body parameter that will be sent as the request body
|
|
50
|
+
*
|
|
51
|
+
* The body parameter represents the main data payload of the request.
|
|
52
|
+
* It is typically used with POST, PUT, and PATCH requests.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```typescript
|
|
56
|
+
* @post('/users')
|
|
57
|
+
* createUser(@body() user: User): Promise<Response>
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
BODY = "body"
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Metadata for method parameters
|
|
64
|
+
*
|
|
65
|
+
* This interface defines the metadata stored for each parameter
|
|
66
|
+
* decorated with @path, @query, @header, or @body decorators.
|
|
67
|
+
*/
|
|
68
|
+
export interface ParameterMetadata {
|
|
69
|
+
/**
|
|
70
|
+
* Type of parameter (path, query, header, body)
|
|
71
|
+
*
|
|
72
|
+
* Specifies how this parameter should be handled in the HTTP request.
|
|
73
|
+
*/
|
|
74
|
+
type: ParameterType;
|
|
75
|
+
/**
|
|
76
|
+
* Name of the parameter (used for path, query, and header parameters)
|
|
77
|
+
*
|
|
78
|
+
* For path and query parameters, this corresponds to the key in the URL.
|
|
79
|
+
* For header parameters, this corresponds to the header name.
|
|
80
|
+
* For body parameters, this is not used.
|
|
81
|
+
*/
|
|
82
|
+
name?: string;
|
|
83
|
+
/**
|
|
84
|
+
* Index of the parameter in the method signature
|
|
85
|
+
*
|
|
86
|
+
* This is used to map the runtime argument values to the correct parameter metadata.
|
|
87
|
+
*/
|
|
88
|
+
index: number;
|
|
89
|
+
}
|
|
90
|
+
export declare const PARAMETER_METADATA_KEY: unique symbol;
|
|
91
|
+
/**
|
|
92
|
+
* Decorator factory for method parameters
|
|
93
|
+
*
|
|
94
|
+
* This function creates a decorator that can be used to specify how a method parameter
|
|
95
|
+
* should be handled in the HTTP request. It stores metadata about the parameter
|
|
96
|
+
* that will be used during request execution.
|
|
97
|
+
*
|
|
98
|
+
* @param type - The type of parameter (PATH, QUERY, HEADER, or BODY)
|
|
99
|
+
* @param name - The name of the parameter (used for path, query, and header parameters)
|
|
100
|
+
* @returns A parameter decorator function
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```typescript
|
|
104
|
+
* @get('/users/{id}')
|
|
105
|
+
* getUser(@parameter(ParameterType.PATH, 'id') userId: string): Promise<Response>
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
export declare function parameter(type: ParameterType, name?: string): (target: any, propertyKey: string, parameterIndex: number) => void;
|
|
109
|
+
export declare function path(name?: string): (target: any, propertyKey: string, parameterIndex: number) => void;
|
|
110
|
+
export declare function query(name?: string): (target: any, propertyKey: string, parameterIndex: number) => void;
|
|
111
|
+
export declare function header(name?: string): (target: any, propertyKey: string, parameterIndex: number) => void;
|
|
112
|
+
export declare function body(): (target: any, propertyKey: string, parameterIndex: number) => void;
|
|
113
|
+
//# sourceMappingURL=parameterDecorator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parameterDecorator.d.ts","sourceRoot":"","sources":["../src/parameterDecorator.ts"],"names":[],"mappings":"AACA,OAAO,kBAAkB,CAAC;AAE1B;;;;;;GAMG;AACH,oBAAY,aAAa;IACvB;;;;;;;;;;;OAWG;IACH,IAAI,SAAS;IAEb;;;;;;;;;;;OAWG;IACH,KAAK,UAAU;IAEf;;;;;;;;;;;OAWG;IACH,MAAM,WAAW;IAEjB;;;;;;;;;;;OAWG;IACH,IAAI,SAAS;CACd;AAED;;;;;GAKG;AACH,MAAM,WAAW,iBAAiB;IAChC;;;;OAIG;IACH,IAAI,EAAE,aAAa,CAAC;IAEpB;;;;;;OAMG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd;;;;OAIG;IACH,KAAK,EAAE,MAAM,CAAC;CACf;AAED,eAAO,MAAM,sBAAsB,eAA+B,CAAC;AAEnE;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,aAAa,EAAE,IAAI,GAAE,MAAW,IAC9C,QAAQ,GAAG,EAAE,aAAa,MAAM,EAAE,gBAAgB,MAAM,UAwBzE;AAED,wBAAgB,IAAI,CAAC,IAAI,GAAE,MAAW,YA1BZ,GAAG,eAAe,MAAM,kBAAkB,MAAM,UA4BzE;AAED,wBAAgB,KAAK,CAAC,IAAI,GAAE,MAAW,YA9Bb,GAAG,eAAe,MAAM,kBAAkB,MAAM,UAgCzE;AAED,wBAAgB,MAAM,CAAC,IAAI,GAAE,MAAW,YAlCd,GAAG,eAAe,MAAM,kBAAkB,MAAM,UAoCzE;AAED,wBAAgB,IAAI,aAtCM,GAAG,eAAe,MAAM,kBAAkB,MAAM,UAwCzE"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extracts parameter names from a function
|
|
3
|
+
*
|
|
4
|
+
* This function parses the string representation of a function to extract
|
|
5
|
+
* the names of its parameters. It handles various function formats including
|
|
6
|
+
* regular functions, arrow functions, and methods.
|
|
7
|
+
*
|
|
8
|
+
* Note: This implementation provides basic parameter name extraction and may not
|
|
9
|
+
* handle all edge cases of complex TypeScript parameter declarations.
|
|
10
|
+
*
|
|
11
|
+
* @param func - The function to extract parameter names from
|
|
12
|
+
* @returns An array of parameter names, or an empty array if extraction fails
|
|
13
|
+
* @throws {TypeError} If the input is not a function
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* function example(a, b, c) {}
|
|
18
|
+
* const paramNames = getParameterNames(example);
|
|
19
|
+
* // Returns: ['a', 'b', 'c']
|
|
20
|
+
*
|
|
21
|
+
* const arrowFunc = (x, y) => x + y;
|
|
22
|
+
* const arrowParamNames = getParameterNames(arrowFunc);
|
|
23
|
+
* // Returns: ['x', 'y']
|
|
24
|
+
*
|
|
25
|
+
* function complex(param1: string, param2: number = 10, ...rest: any[]) {}
|
|
26
|
+
* const complexParamNames = getParameterNames(complex);
|
|
27
|
+
* // Returns: ['param1', 'param2', '...rest']
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export declare function getParameterNames(func: Function): string[];
|
|
31
|
+
/**
|
|
32
|
+
* Helper function to automatically extract parameter name when not provided
|
|
33
|
+
*
|
|
34
|
+
* @param target - The target object (class prototype)
|
|
35
|
+
* @param propertyKey - The method name
|
|
36
|
+
* @param parameterIndex - The index of the parameter
|
|
37
|
+
* @param providedName - The name explicitly provided by the user (if any)
|
|
38
|
+
* @returns The parameter name, either provided or automatically extracted
|
|
39
|
+
*/
|
|
40
|
+
export declare function getParameterName(target: any, propertyKey: string, parameterIndex: number, providedName?: string): string | undefined;
|
|
41
|
+
//# sourceMappingURL=reflection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reflection.d.ts","sourceRoot":"","sources":["../src/reflection.ts"],"names":[],"mappings":"AAaA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,QAAQ,GAAG,MAAM,EAAE,CAwB1D;AAED;;;;;;;;GAQG;AACH,wBAAgB,gBAAgB,CAC9B,MAAM,EAAE,GAAG,EACX,WAAW,EAAE,MAAM,EACnB,cAAc,EAAE,MAAM,EACtB,YAAY,CAAC,EAAE,MAAM,GACpB,MAAM,GAAG,SAAS,CAqBpB"}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { Fetcher, FetcherRequest, NamedCapable } from '@ahoo-wang/fetcher';
|
|
2
|
+
import { ApiMetadata } from './apiDecorator';
|
|
3
|
+
import { EndpointMetadata } from './endpointDecorator';
|
|
4
|
+
import { ParameterMetadata } from './parameterDecorator';
|
|
5
|
+
/**
|
|
6
|
+
* Metadata container for a function with HTTP endpoint decorators
|
|
7
|
+
*
|
|
8
|
+
* This class encapsulates all the metadata needed to execute an HTTP request
|
|
9
|
+
* for a decorated method, including API-level defaults, endpoint-specific
|
|
10
|
+
* configuration, and parameter metadata.
|
|
11
|
+
*/
|
|
12
|
+
export declare class FunctionMetadata implements NamedCapable {
|
|
13
|
+
/**
|
|
14
|
+
* Name of the function
|
|
15
|
+
*/
|
|
16
|
+
name: string;
|
|
17
|
+
/**
|
|
18
|
+
* API-level metadata (class-level configuration)
|
|
19
|
+
*/
|
|
20
|
+
api: ApiMetadata;
|
|
21
|
+
/**
|
|
22
|
+
* Endpoint-level metadata (method-level configuration)
|
|
23
|
+
*/
|
|
24
|
+
endpoint: EndpointMetadata;
|
|
25
|
+
/**
|
|
26
|
+
* Parameter metadata for all decorated parameters
|
|
27
|
+
*/
|
|
28
|
+
parameters: ParameterMetadata[];
|
|
29
|
+
/**
|
|
30
|
+
* Creates a new FunctionMetadata instance
|
|
31
|
+
*
|
|
32
|
+
* @param name - The name of the function
|
|
33
|
+
* @param api - API-level metadata
|
|
34
|
+
* @param endpoint - Endpoint-level metadata
|
|
35
|
+
* @param parameters - Parameter metadata array
|
|
36
|
+
*/
|
|
37
|
+
constructor(name: string, api: ApiMetadata, endpoint: EndpointMetadata, parameters: ParameterMetadata[]);
|
|
38
|
+
/**
|
|
39
|
+
* Gets the fetcher instance to use for this function
|
|
40
|
+
*
|
|
41
|
+
* Returns the fetcher specified in the endpoint metadata, or the API metadata,
|
|
42
|
+
* or falls back to the default fetcher if none is specified.
|
|
43
|
+
*
|
|
44
|
+
* @returns The fetcher instance
|
|
45
|
+
*/
|
|
46
|
+
get fetcher(): Fetcher;
|
|
47
|
+
/**
|
|
48
|
+
* Resolves the request configuration from the method arguments
|
|
49
|
+
*
|
|
50
|
+
* This method processes the runtime arguments according to the parameter metadata
|
|
51
|
+
* and constructs a FetcherRequest object with path parameters, query parameters,
|
|
52
|
+
* headers, body, and timeout.
|
|
53
|
+
*
|
|
54
|
+
* @param args - The runtime arguments passed to the method
|
|
55
|
+
* @returns A FetcherRequest object with all request configuration
|
|
56
|
+
*/
|
|
57
|
+
resolveRequest(args: any[]): FetcherRequest;
|
|
58
|
+
/**
|
|
59
|
+
* Resolves the full path for the request
|
|
60
|
+
*
|
|
61
|
+
* Combines the base path from API metadata with the endpoint path
|
|
62
|
+
* from endpoint metadata to create the complete path.
|
|
63
|
+
*
|
|
64
|
+
* @returns The full path for the request
|
|
65
|
+
*/
|
|
66
|
+
resolvePath(): string;
|
|
67
|
+
/**
|
|
68
|
+
* Resolves the timeout for the request
|
|
69
|
+
*
|
|
70
|
+
* Returns the timeout specified in the endpoint metadata, or the API metadata,
|
|
71
|
+
* or undefined if no timeout is specified.
|
|
72
|
+
*
|
|
73
|
+
* @returns The timeout value in milliseconds, or undefined
|
|
74
|
+
*/
|
|
75
|
+
resolveTimeout(): number | undefined;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Executor for HTTP requests based on decorated method metadata
|
|
79
|
+
*
|
|
80
|
+
* This class is responsible for executing HTTP requests based on the metadata
|
|
81
|
+
* collected from decorators. It resolves the path, constructs the request,
|
|
82
|
+
* and executes it using the appropriate fetcher.
|
|
83
|
+
*/
|
|
84
|
+
export declare class RequestExecutor {
|
|
85
|
+
private readonly metadata;
|
|
86
|
+
/**
|
|
87
|
+
* Creates a new RequestExecutor instance
|
|
88
|
+
*
|
|
89
|
+
* @param metadata - The function metadata containing all request information
|
|
90
|
+
*/
|
|
91
|
+
constructor(metadata: FunctionMetadata);
|
|
92
|
+
/**
|
|
93
|
+
* Executes the HTTP request
|
|
94
|
+
*
|
|
95
|
+
* This method resolves the path and request configuration from the metadata
|
|
96
|
+
* and arguments, then executes the request using the configured fetcher.
|
|
97
|
+
*
|
|
98
|
+
* @param args - The runtime arguments passed to the method
|
|
99
|
+
* @returns A Promise that resolves to the Response
|
|
100
|
+
*/
|
|
101
|
+
execute(args: any[]): Promise<Response>;
|
|
102
|
+
}
|
|
103
|
+
//# sourceMappingURL=requestExecutor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"requestExecutor.d.ts","sourceRoot":"","sources":["../src/requestExecutor.ts"],"names":[],"mappings":"AAYA,OAAO,EAGL,OAAO,EAEP,cAAc,EACd,YAAY,EACb,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AACvD,OAAO,EAAE,iBAAiB,EAAiB,MAAM,sBAAsB,CAAC;AAExE;;;;;;GAMG;AACH,qBAAa,gBAAiB,YAAW,YAAY;IACnD;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;OAEG;IACH,GAAG,EAAE,WAAW,CAAC;IAEjB;;OAEG;IACH,QAAQ,EAAE,gBAAgB,CAAC;IAE3B;;OAEG;IACH,UAAU,EAAE,iBAAiB,EAAE,CAAC;IAEhC;;;;;;;OAOG;gBAED,IAAI,EAAE,MAAM,EACZ,GAAG,EAAE,WAAW,EAChB,QAAQ,EAAE,gBAAgB,EAC1B,UAAU,EAAE,iBAAiB,EAAE;IAQjC;;;;;;;OAOG;IACH,IAAI,OAAO,IAAI,OAAO,CAIrB;IAED;;;;;;;;;OASG;IACH,cAAc,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,cAAc;IA8C3C;;;;;;;OAOG;IACH,WAAW,IAAI,MAAM;IAMrB;;;;;;;OAOG;IACH,cAAc,IAAI,MAAM,GAAG,SAAS;CAGrC;AAED;;;;;;GAMG;AACH,qBAAa,eAAe;IAC1B,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAmB;IAE5C;;;;OAIG;gBACS,QAAQ,EAAE,gBAAgB;IAItC;;;;;;;;OAQG;IACG,OAAO,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC,QAAQ,CAAC;CAK9C"}
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ahoo-wang/fetcher-decorator",
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"description": "Decorator support for Fetcher HTTP client",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"fetch",
|
|
7
|
+
"http",
|
|
8
|
+
"client",
|
|
9
|
+
"decorator",
|
|
10
|
+
"typescript"
|
|
11
|
+
],
|
|
12
|
+
"author": "Ahoo-Wang",
|
|
13
|
+
"license": "Apache-2.0",
|
|
14
|
+
"homepage": "https://github.com/Ahoo-Wang/fetcher/tree/master/packages/fetcher-decorator",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "https://github.com/Ahoo-Wang/fetcher.git",
|
|
18
|
+
"directory": "packages/fetcher-decorator"
|
|
19
|
+
},
|
|
20
|
+
"bugs": {
|
|
21
|
+
"url": "https://github.com/Ahoo-Wang/fetcher/issues"
|
|
22
|
+
},
|
|
23
|
+
"main": "./dist/index.umd.js",
|
|
24
|
+
"module": "./dist/index.es.js",
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"import": "./dist/index.es.js",
|
|
30
|
+
"require": "./dist/index.umd.js"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"files": [
|
|
34
|
+
"dist"
|
|
35
|
+
],
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"reflect-metadata": "^0.2.2",
|
|
38
|
+
"@ahoo-wang/fetcher": "0.5.0"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@vitest/coverage-v8": "^3.2.4",
|
|
42
|
+
"eslint": "^9.33.0",
|
|
43
|
+
"prettier": "^3.6.2",
|
|
44
|
+
"typescript": "^5.9.2",
|
|
45
|
+
"unplugin-dts": "1.0.0-beta.6",
|
|
46
|
+
"vite": "^7.1.3",
|
|
47
|
+
"vitest": "^3.2.4"
|
|
48
|
+
},
|
|
49
|
+
"scripts": {
|
|
50
|
+
"build": "vite build",
|
|
51
|
+
"test": "vitest run --coverage",
|
|
52
|
+
"lint": "tsc --noEmit",
|
|
53
|
+
"clean": "rm -rf dist"
|
|
54
|
+
}
|
|
55
|
+
}
|