@redocly/framework-plugin-apigee 0.1.0-next.0 → 0.1.0-next.10
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/CHANGELOG.md +194 -0
- package/README.md +6 -6
- package/lib/.tsbuildinfo +1 -1
- package/lib/client/ApigeeTemplate.d.ts +3 -1
- package/lib/client/ApigeeTemplate.js +1 -1
- package/lib/client/Primitives.d.ts +54 -32
- package/lib/client/Primitives.js +274 -126
- package/lib/client/api.d.ts +0 -1
- package/lib/client/api.js +1 -1
- package/lib/client/context.d.ts +3 -1
- package/lib/client/hooks.d.ts +7 -10
- package/lib/client/hooks.js +1 -1
- package/lib/client/queries.d.ts +28 -0
- package/lib/client/queries.js +1 -0
- package/lib/client/views/AdminView.d.ts +3 -0
- package/lib/client/views/AdminView.js +13 -18
- package/lib/client/views/AppsView.d.ts +7 -4
- package/lib/client/views/AppsView.js +1 -1
- package/lib/client/views/Dialogs.d.ts +10 -1
- package/lib/client/views/Dialogs.js +9 -1
- package/lib/client/views/TeamsView.d.ts +5 -7
- package/lib/client/views/TeamsView.js +1 -1
- package/lib/constants.d.ts +1 -1
- package/lib/constants.js +1 -1
- package/lib/contract.d.ts +13 -0
- package/lib/plugin.d.ts +1 -1
- package/lib/plugin.js +1 -1
- package/lib/server/service.d.ts +5 -2
- package/lib/server/service.js +1 -1
- package/package.json +7 -6
package/lib/server/service.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var u=function(s,e,i,t){function r(n){return n instanceof i?n:new i(function(o){o(n)})}return new(i||(i=Promise))(function(n,o){function a(l){try{c(t.next(l))}catch(p){o(p)}}function d(l){try{c(t.throw(l))}catch(p){o(p)}}function c(l){l.done?n(l.value):r(l.value).then(a,d)}c((t=t.apply(s,e||[])).next())})},x=function(s,e,i,t){if(i==="a"&&!t)throw new TypeError("Private accessor was defined without a getter");if(typeof e=="function"?s!==e||!t:!e.has(s))throw new TypeError("Cannot read private member from an object whose class did not declare it");return i==="m"?t:i==="a"?t.call(s):t?t.value:e.get(s)},B=function(s,e,i,t,r){if(t==="m")throw new TypeError("Private method is not writable");if(t==="a"&&!r)throw new TypeError("Private accessor was defined without a setter");if(typeof e=="function"?s!==e||!r:!e.has(s))throw new TypeError("Cannot write private member to an object whose class did not declare it");return t==="a"?r.call(s,i):r?r.value=i:e.set(s,i),i},D=function(s,e){var i={};for(var t in s)Object.prototype.hasOwnProperty.call(s,t)&&e.indexOf(t)<0&&(i[t]=s[t]);if(s!=null&&typeof Object.getOwnPropertySymbols=="function")for(var r=0,t=Object.getOwnPropertySymbols(s);r<t.length;r++)e.indexOf(t[r])<0&&Object.prototype.propertyIsEnumerable.call(s,t[r])&&(i[t[r]]=s[t[r]]);return i},R,j;import{createHash as ie,randomBytes as re}from"node:crypto";import{isApiProductOffered as C}from"../config.js";import{APIGEE_DEFAULT_KEY_LIFETIME_SECONDS as $,APIGEE_DISPLAY_NAME_ATTRIBUTE as I,APIGEE_NOTES_ATTRIBUTE as k,APIGEE_PRODUCTS_ENTITY_KEY as ne,APIGEE_PRODUCTS_VALUE_KEY as oe,APIGEE_TEAM_MEMBERS_ATTRIBUTE as F}from"../constants.js";import{maskKey as L}from"../contract.js";import{ApigeeApiError as se}from"./client.js";import{attributesToRecord as O,recordToAttributes as K,toApiProduct as V,toApp as N,toCredential as z}from"./mappers.js";import{ApigeeStore as ae,normalizeEmail as _}from"./store.js";class v extends Error{constructor(){super(...arguments),Object.defineProperty(this,"name",{enumerable:!0,configurable:!0,writable:!0,value:"ValidationError"})}}class S extends Error{constructor(){super(...arguments),Object.defineProperty(this,"name",{enumerable:!0,configurable:!0,writable:!0,value:"ForbiddenError"})}}class b extends Error{constructor(){super(...arguments),Object.defineProperty(this,"name",{enumerable:!0,configurable:!0,writable:!0,value:"NotFoundError"})}}const de={byApiId:{},byTitle:{}},E={none:0,read:1,triage:2,write:3,maintain:4,admin:5};function T(s){return E[s]>=E.triage}function G(s){return E[s]>=E.maintain}function le(s,e){return E[s]>=E[e]?s:e}const ce=/^[A-Za-z0-9._$ -]{1,100}$/,ue=new Set(["teams","admin"]),pe=/^[a-z0-9][a-z0-9-]{1,62}$/,P=2e3,me=2048;function he(s){return Buffer.from(JSON.stringify(s)).toString("base64url")}function Y(s){return ie("sha256").update(s).digest("base64url")}function fe(s){try{const e=JSON.parse(Buffer.from(s,"base64url").toString());if(!e||typeof e.appId!="string"||typeof e.keyDigest!="string"||typeof e.apiProduct!="string"||!e.owner)throw new Error("shape");return e}catch{throw new v("Unknown request")}}function ve(s){return s.trim().toLowerCase().split(/[^a-z0-9]/).filter(Boolean).join("-").slice(0,63)}function we(s){var e,i,t,r;const n=((i=s.name)!==null&&i!==void 0?i:"").trim().split(/\s+/).filter(Boolean),o=(r=(e=((t=s.email)!==null&&t!==void 0?t:"").split("@")[0])===null||e===void 0?void 0:e.split(/[.-]/))!==null&&r!==void 0?r:[];return{firstName:n[0]||o[0]||"Unknown",lastName:n.slice(1).join(" ")||o[1]||"Unknown"}}function H(s){return re(s).toString("base64url")}function J(s,e){const i=O(s.attributes)[F];if(!i)return[];try{const t=JSON.parse(i);return Array.isArray(t)?t.flatMap(r=>{var n,o;if(typeof r?.developer!="string")return[];const a=_(r.developer),d=e?.members.find(c=>c.email===a);return[{email:a,role:!((n=r.roles)===null||n===void 0)&&n.includes("owner")?"owner":"member",addedAt:(o=d?.addedAt)!==null&&o!==void 0?o:Number(s.createdAt)||0}]}):[]}catch{return[]}}function U(s,e){var i,t,r,n;const o=J(s,e);return{id:s.name,name:s.displayName||s.name,description:(t=O(s.attributes)[k])!==null&&t!==void 0?t:"",createdAt:Number(s.createdAt)||e?.createdAt||Date.now(),createdBy:(n=(r=e?.createdBy)!==null&&r!==void 0?r:(i=o.find(a=>a.role==="owner"))===null||i===void 0?void 0:i.email)!==null&&n!==void 0?n:"",members:o}}function W(s){return JSON.stringify(s.map(e=>({developer:e.email,roles:[e.role]})))}class Te{constructor(e,i,t,r,n){Object.defineProperty(this,"settings",{enumerable:!0,configurable:!0,writable:!0,value:e}),Object.defineProperty(this,"client",{enumerable:!0,configurable:!0,writable:!0,value:i}),Object.defineProperty(this,"viewer",{enumerable:!0,configurable:!0,writable:!0,value:t}),Object.defineProperty(this,"ctx",{enumerable:!0,configurable:!0,writable:!0,value:r}),Object.defineProperty(this,"logger",{enumerable:!0,configurable:!0,writable:!0,value:n}),Object.defineProperty(this,"store",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),R.set(this,void 0),j.set(this,void 0),this.store=new ae(r.kv)}get email(){return _(this.viewer.email)}apigeeProducts(){var e;return B(this,R,(e=x(this,R,"f"))!==null&&e!==void 0?e:this.client.listApiProducts(),"f"),x(this,R,"f")}productPages(){var e;return B(this,j,(e=x(this,j,"f"))!==null&&e!==void 0?e:this.ctx.entities.load(ne).then(i=>{var t;return(t=i?.[oe])!==null&&t!==void 0?t:de}),"f"),x(this,j,"f")}pageFor(e,i){const t=i.byApiId[e.name];if(t||!this.settings.apiProducts.matchByTitle)return t;const r=(e.displayName||e.name).trim().toLowerCase();return i.byTitle[r]}roleFor(e,i,t){const r=t.find(a=>a.name===e),n=r?this.pageFor(r,i):i.byApiId[e],o=n?this.ctx.roleForSlug(n.slug):"none";return le(o,this.ctx.roleForSlug(this.settings.basePath))}approvalSummary(){return u(this,void 0,void 0,function*(){if(!this.settings.approvalsEnabled)return{enabled:!1,canView:!1,canDecide:!1};const e=this.ctx.roleForSlug(this.settings.basePath);if(G(e))return{enabled:!0,canView:!0,canDecide:!0};const[i,t]=yield Promise.all([this.apigeeProducts(),this.productPages()]);let r=T(e),n=!1;for(const o of i){if(!C(this.settings,o.name))continue;const a=this.roleFor(o.name,t,i);r||(r=T(a)),n||(n=G(a))}return{enabled:!0,canView:r,canDecide:n}})}canReview(){return u(this,void 0,void 0,function*(){return(yield this.approvalSummary()).canView})}listApiProducts(){return u(this,void 0,void 0,function*(){const[e,i]=yield Promise.all([this.apigeeProducts(),this.productPages()]),t=[];for(const r of e){if(!C(this.settings,r.name))continue;const n=this.pageFor(r,i);if(n){if(!this.ctx.isSlugVisible(n.slug))continue;t.push(V(r,n))}else this.settings.apiProducts.allowOutsideCatalog&&t.push(V(r))}return t.sort((r,n)=>r.name.localeCompare(n.name))})}assertProductsAllowed(e){return u(this,void 0,void 0,function*(){const i=Z(e),t=yield this.listApiProducts(),r=i.filter(o=>!t.some(a=>a.id===o));if(r.length>0)throw new v(`Unknown API product: ${r.join(", ")}`);const n=new Map(t.map(o=>[o.id,o]));return[...new Set(i)].flatMap(o=>{const a=n.get(o);return a?[a]:[]})})}resolveOwner(e){return u(this,void 0,void 0,function*(){if(!e)return{owner:{kind:"developer",email:this.email},path:{kind:"developer",email:this.email}};const i=yield this.requireTeam(e);return{owner:{kind:"team",teamId:i.id},path:{kind:"appgroup",name:i.id}}})}ownerPath(e){return e.kind==="developer"?{kind:"developer",email:e.email}:{kind:"appgroup",name:e.teamId}}withDecisions(e){return u(this,void 0,void 0,function*(){const i=[];for(const n of e)for(const o of n.credentials)for(const a of o.apiProducts)i.push({owner:n.owner,appId:n.id,consumerKey:o.consumerKey,apiProduct:a.id});const t=yield this.store.getDecisions(i);let r=0;for(const n of e)for(const o of n.credentials)for(const a of o.apiProducts){const d=t[r++];d&&(a.decision=d)}return e})}listApps(){return u(this,void 0,void 0,function*(){return this.appsOf(yield this.listMyTeams())})}appsOf(e){return u(this,void 0,void 0,function*(){const[i,...t]=yield Promise.all([this.client.listApps({kind:"developer",email:this.email}),...e.map(n=>this.client.listApps({kind:"appgroup",name:n.id}))]),r=i.map(n=>N(n,{kind:"developer",email:this.email}));return e.forEach((n,o)=>{r.push(...t[o].map(a=>N(a,{kind:"team",teamId:n.id})))}),this.withDecisions(r.sort((n,o)=>{var a,d;return((a=o.createdAt)!==null&&a!==void 0?a:0)-((d=n.createdAt)!==null&&d!==void 0?d:0)}))})}createApp(e){return u(this,void 0,void 0,function*(){var i;const t=Q(e.name),r=g(e.description,P,"description"),n=ee(e.callbackUrl),o=g(e.note,P,"note"),a=yield this.assertProductsAllowed((i=e.apiProducts)!==null&&i!==void 0?i:[]),{owner:d,path:c}=yield this.resolveOwner(e.teamId);if(d.kind==="developer"&&(yield this.client.ensureDeveloper(Object.assign({email:this.email,userName:this.email},we(this.viewer)))),yield this.client.getApp(c,t))throw new v(`An app named "${t}" already exists`);const l=yield this.client.createApp(c,Object.assign(Object.assign({name:t,apiProducts:a.map(h=>h.id)},n?{callbackUrl:n}:{}),{keyExpiresIn:String($*(d.kind==="developer"?1e3:1)),attributes:K({[I]:t,[k]:r??""})})),p=N(l,d);return yield this.recordRequestNotes(d,p.id,p.credentials,a,o),p})}updateApp(e,i,t){return u(this,void 0,void 0,function*(){var r;const{owner:n,path:o}=yield this.resolveOwner(e),a=yield this.client.getApp(o,i);if(!a)throw new b("App not found");const d=O(a.attributes);t.name!==void 0&&(d[I]=Q(t.name)),t.description!==void 0&&(d[k]=(r=g(t.description,P,"description"))!==null&&r!==void 0?r:"");const c=t.callbackUrl===void 0?a.callbackUrl:ee(t.callbackUrl),l=yield this.client.updateApp(o,i,Object.assign({attributes:K(d)},c!==void 0?{callbackUrl:c}:{}));return N(l,n)})}deleteApp(e,i){return u(this,void 0,void 0,function*(){const{path:t}=yield this.resolveOwner(e);if(!(yield this.client.getApp(t,i)))throw new b("App not found");yield this.client.deleteApp(t,i)})}createKey(e,i,t){return u(this,void 0,void 0,function*(){var r;const{owner:n,path:o}=yield this.resolveOwner(e);if(!(yield this.client.getApp(o,i)))throw new b("App not found");const a=yield this.assertProductsAllowed((r=t.apiProducts)!==null&&r!==void 0?r:[]),d=g(t.note,P,"note"),c=yield this.client.createKey(o,i,{consumerKey:H(24),consumerSecret:H(24),expiresInSeconds:$}),l=a.length>0?yield this.client.addKeyProducts(o,i,c.consumerKey,a.map(h=>h.id),c.attributes):c,p=z(l);return yield this.recordRequestNotes(n,i,[p],a,d),p})}updateKeyProducts(e,i,t,r){return u(this,void 0,void 0,function*(){var n,o;const{owner:a,path:d}=yield this.resolveOwner(e),c=yield this.client.getKey(d,i,t);if(!c)throw new b("Key not found");const l=new Set(((n=c.apiProducts)!==null&&n!==void 0?n:[]).map(y=>y.apiproduct)),p=Z((o=r.apiProducts)!==null&&o!==void 0?o:[]),h=yield this.assertProductsAllowed(p.filter(y=>!l.has(y))),w=g(r.note,P,"note"),m=[...l].filter(y=>!p.includes(y));let f=c;h.length>0&&(f=yield this.client.addKeyProducts(d,i,t,h.map(y=>y.id),c.attributes));for(const y of m)yield this.client.removeKeyProduct(d,i,t,y),yield this.store.deleteRequestNote({owner:a,appId:i,consumerKey:t,apiProduct:y}),yield this.store.deleteDecision({owner:a,appId:i,consumerKey:t,apiProduct:y});const A=m.length>0?yield this.client.getKey(d,i,t):null,q=z(A??f);return yield this.recordRequestNotes(a,i,[q],h,w),q})}deleteKey(e,i,t){return u(this,void 0,void 0,function*(){const{path:r}=yield this.resolveOwner(e);if(!(yield this.client.getKey(r,i,t)))throw new b("Key not found");yield this.client.deleteKey(r,i,t)})}recordRequestNotes(e,i,t,r,n){return u(this,void 0,void 0,function*(){const o=r.filter(d=>d.approvalType==="manual");if(o.length===0)return;const a=Date.now();for(const d of t)for(const c of o)yield this.store.putRequestNote({owner:e,appId:i,consumerKey:d.consumerKey,apiProduct:c.id},Object.assign({requestedBy:this.email,requestedAt:a},n?{note:n}:{}))})}getAccess(e){return u(this,void 0,void 0,function*(){const i=yield this.appsOf(yield this.indexedTeams()),t=Date.now(),r=o=>i.flatMap(a=>a.credentials.filter(d=>a.status!=="revoked"&&d.status==="approved"&&(d.expiresAt===null||d.expiresAt>t)&&d.apiProducts.some(c=>c.id===e&&c.status===o)).map(d=>({app:a,credential:d}))),n=r("approved").sort((o,a)=>o.app.owner.kind===a.app.owner.kind?0:o.app.owner.kind==="developer"?-1:1);if(n.length>0){const{credential:o}=n[0];return{status:"APPROVED",link:this.settings.basePath,requestValues:{security:{default:Object.assign({client_id:o.consumerKey},o.consumerSecret?{client_secret:o.consumerSecret}:{})}}}}return{status:r("pending").length>0?"PENDING":"NONE",link:`${this.settings.basePath}?createApp&apiProduct=${encodeURIComponent(e)}`}})}assertTeams(){if(!this.settings.teamsEnabled)throw new S("Teams are disabled")}withRole(e){const i=e.members.find(t=>t.email===this.email);return i?Object.assign(Object.assign({},e),{myRole:i.role}):e}listMyTeams(){return u(this,void 0,void 0,function*(){var e,i;if(!this.settings.teamsEnabled)return[];let t;try{t=yield this.client.listAppGroups()}catch(o){return(e=this.logger)===null||e===void 0||e.debug(`apigee: AppGroups unavailable: ${String(o)}`),[]}const r=new Map((yield this.store.listTeamsOf(this.email)).map(o=>[o.id,o])),n=[];for(const o of t){if(!J(o).some(d=>d.email===this.email))continue;const a=U(o,(i=r.get(o.name))!==null&&i!==void 0?i:yield this.store.getTeam(o.name));r.has(a.id)||(yield this.store.putTeam(a)),n.push(this.withRole(a))}return n.sort((o,a)=>o.name.localeCompare(a.name))})}indexedTeams(){return u(this,void 0,void 0,function*(){if(!this.settings.teamsEnabled)return[];const e=yield this.store.listTeamsOf(this.email);return(yield Promise.all(e.map(t=>this.client.getAppGroup(t.id)))).flatMap((t,r)=>{const n=t?this.withRole(U(t,e[r])):null;return n?.myRole?[n]:[]})})}requireTeam(e,i){return u(this,void 0,void 0,function*(){this.assertTeams();const[t,r]=yield Promise.all([this.client.getAppGroup(e),this.store.getTeam(e)]);if(!t)throw new b("Team not found");const n=this.withRole(U(t,r));if(!n.myRole)throw new S("You are not a member of this team");if(i==="owner"&&n.myRole!=="owner")throw new S("Only a team owner can do this");return n})}getTeam(e){return u(this,void 0,void 0,function*(){return this.requireTeam(e)})}createTeam(e){return u(this,void 0,void 0,function*(){var i;this.assertTeams();const t=g(e.name,100,"name");if(!t)throw new v("A team needs a name");const r=ve(t);if(!pe.test(r))throw new v("The team name needs at least two letters or digits");const n=(i=g(e.description,P,"description"))!==null&&i!==void 0?i:"";if((yield this.store.getTeam(r))||(yield this.client.getAppGroup(r)))throw new v(`A team named "${t}" already exists`);const o=[{email:this.email,role:"owner",addedAt:Date.now()}];yield this.client.createAppGroup({name:r,displayName:t,attributes:K({[k]:n,[F]:W(o)})});const a={id:r,name:t,description:n,createdAt:Date.now(),createdBy:this.email,members:o};return yield this.store.putTeam(a),this.withRole(a)})}updateTeam(e,i){return u(this,void 0,void 0,function*(){var t;const r=yield this.requireTeam(e,"owner"),n=g(i.name,100,"name")||r.name,o=(t=g(i.description,P,"description"))!==null&&t!==void 0?t:r.description,a=Object.assign(Object.assign({},M(r)),{name:n,description:o});return yield this.syncAppGroup(a),yield this.store.putTeam(a),this.withRole(a)})}deleteTeam(e){return u(this,void 0,void 0,function*(){const i=yield this.requireTeam(e,"owner"),t={kind:"appgroup",name:i.id};for(const r of yield this.client.listApps(t))yield this.client.deleteApp(t,r.name);try{yield this.client.deleteAppGroup(i.id)}catch(r){if(!(r instanceof se&&r.status===404))throw r}yield this.store.deleteTeam(i.id)})}addMember(e,i){return u(this,void 0,void 0,function*(){const t=yield this.requireTeam(e,"owner"),r=ye(i.email),n=te(i.role);if(t.members.some(a=>a.email===r))throw new v(`${r} is already a member`);const o=Object.assign(Object.assign({},M(t)),{members:[...t.members,{email:r,role:n,addedAt:Date.now()}]});return yield this.syncAppGroup(o),yield this.store.putTeam(o),this.withRole(o)})}setMemberRole(e,i,t){return u(this,void 0,void 0,function*(){const r=yield this.requireTeam(e,"owner"),n=_(i);if(!r.members.some(d=>d.email===n))throw new b("Member not found");const o=r.members.map(d=>d.email===n?Object.assign(Object.assign({},d),{role:te(t)}):d);X(o);const a=Object.assign(Object.assign({},M(r)),{members:o});return yield this.syncAppGroup(a),yield this.store.putTeam(a),this.withRole(a)})}removeMember(e,i){return u(this,void 0,void 0,function*(){const t=yield this.requireTeam(e),r=_(i);if(t.myRole!=="owner"&&r!==this.email)throw new S("Only a team owner can remove other members");if(!t.members.some(a=>a.email===r))throw new b("Member not found");const n=t.members.filter(a=>a.email!==r);X(n);const o=Object.assign(Object.assign({},M(t)),{members:n});return yield this.syncAppGroup(o),yield this.store.putTeam(o),this.withRole(o)})}syncAppGroup(e){return u(this,void 0,void 0,function*(){const i=yield this.client.getAppGroup(e.id);if(!i)throw new b("Team not found");const t=Object.assign(Object.assign({},O(i.attributes)),{[k]:e.description,[F]:W(e.members)});yield this.client.updateAppGroup(e.id,Object.assign(Object.assign({},i),{name:e.id,displayName:e.name,attributes:K(t)}))})}ownerOf(e,i){return u(this,void 0,void 0,function*(){var t;if(e.appGroup)return{kind:"team",teamId:e.appGroup};if(e.developerEmail)return{kind:"developer",email:_(e.developerEmail)};if(e.developerId){let r=i.get(e.developerId);if(r||(r=(t=yield this.client.getDeveloper(e.developerId))===null||t===void 0?void 0:t.email,r&&i.set(e.developerId,r)),r)return{kind:"developer",email:_(r)}}return null})}listRequests(){return u(this,void 0,void 0,function*(){var e,i;const[t,r,n]=yield Promise.all([this.client.listOrganizationApps(),this.apigeeProducts(),this.productPages()]),o=new Map,a=[];for(const d of t){const c=((e=d.credentials)!==null&&e!==void 0?e:[]).flatMap(h=>{var w;return((w=h.apiProducts)!==null&&w!==void 0?w:[]).filter(m=>m.status==="pending").filter(m=>T(this.roleFor(m.apiproduct,n,r))).map(m=>({credential:h,apiProduct:m.apiproduct}))});if(c.length===0)continue;const l=yield this.ownerOf(d,o);if(!l)continue;const p=O(d.attributes)[I];for(const{credential:h,apiProduct:w}of c){const{consumerKey:m}=h,f=yield this.store.getRequestNote({owner:l,appId:d.name,consumerKey:m,apiProduct:w});a.push(Object.assign(Object.assign(Object.assign(Object.assign({id:he({owner:l,appId:d.name,keyDigest:Y(m),apiProduct:w}),owner:l},f?{requestedBy:f.requestedBy}:{}),{appId:d.name,appName:p||d.name,consumerKey:L(m),apiProduct:w}),f?.note?{note:f.note}:{}),{requestedAt:(i=f?.requestedAt)!==null&&i!==void 0?i:Number(h.issuedAt)||null,canDecide:G(this.roleFor(w,n,r))}))}}return a.sort((d,c)=>{var l,p;return((l=c.requestedAt)!==null&&l!==void 0?l:0)-((p=d.requestedAt)!==null&&p!==void 0?p:0)})})}decideRequest(e,i,t){return u(this,void 0,void 0,function*(){var r;const n=fe(e),{keyDigest:o}=n,a=D(n,["keyDigest"]),[d,c]=yield Promise.all([this.apigeeProducts(),this.productPages()]);if(!G(this.roleFor(a.apiProduct,c,d)))throw new S("You cannot decide requests for this API product");const l=g(t,P,"note"),p=this.ownerPath(a.owner),h=yield this.client.getApp(p,a.appId);if(!h)throw new b("App not found");const w=(r=h.credentials)===null||r===void 0?void 0:r.find(A=>Y(A.consumerKey)===o);if(!w)throw new b("Key not found");const m=Object.assign(Object.assign({},a),{consumerKey:w.consumerKey});yield this.client.setKeyProductStatus(p,m.appId,m.consumerKey,m.apiProduct,i),yield this.store.deleteRequestNote(m);const f=Object.assign({action:i,actor:this.email,at:Date.now()},l?{note:l}:{});return yield this.store.putDecision(m,f),this.store.appendAudit(Object.assign({at:f.at,actor:this.email,action:i,owner:m.owner,appId:m.appId,appName:O(h.attributes)[I]||h.name,consumerKey:L(m.consumerKey),apiProduct:m.apiProduct},l?{note:l}:{}))})}listAllApps(){return u(this,void 0,void 0,function*(){var e;const[i,t,r]=yield Promise.all([this.client.listOrganizationApps(),this.apigeeProducts(),this.productPages()]),n=T(this.ctx.roleForSlug(this.settings.basePath)),o=new Map,a=[];for(const d of i){if(!(n||((e=d.credentials)!==null&&e!==void 0?e:[]).some(m=>{var f;return((f=m.apiProducts)!==null&&f!==void 0?f:[]).some(A=>T(this.roleFor(A.apiproduct,r,t)))})))continue;const l=yield this.ownerOf(d,o);if(!l)continue;const p=N(d,l),{credentials:h}=p,w=D(p,["credentials"]);a.push(Object.assign(Object.assign({},w),{credentials:h.map(m=>{var{consumerKey:f,consumerSecret:A}=m,q=D(m,["consumerKey","consumerSecret"]);return q})}))}return a.sort((d,c)=>{var l,p;return((l=c.createdAt)!==null&&l!==void 0?l:0)-((p=d.createdAt)!==null&&p!==void 0?p:0)})})}listAudit(){return u(this,void 0,void 0,function*(){const[e,i,t]=yield Promise.all([this.store.listAudit(),this.apigeeProducts(),this.productPages()]);return e.filter(r=>T(this.roleFor(r.apiProduct,t,i)))})}}R=new WeakMap,j=new WeakMap;function M(s){const{myRole:e}=s;return D(s,["myRole"])}function X(s){if(!s.some(e=>e.role==="owner"))throw new v("A team needs at least one owner")}function Z(s){if(!Array.isArray(s)||s.some(e=>typeof e!="string"))throw new v("apiProducts must be a list of product names");return s}function Q(s){if(typeof s!="string"||!ce.test(s.trim()))throw new v("An app name has 1 to 100 letters, digits, spaces, dots, dashes or underscores");const e=s.trim();if(ue.has(e))throw new v(`An app cannot be named "${e}"`);return e}function g(s,e,i){if(s==null)return;if(typeof s!="string")throw new v(`${i} must be text`);const t=s.trim();if(t.length>e)throw new v(`${i} is longer than ${e} characters`);return t}function ee(s){const e=g(s,me,"callbackUrl");if(!e)return e===""?"":void 0;for(const i of e.split(","))try{new URL(i.trim())}catch{throw new v(`"${i.trim()}" is not a valid callback URL`)}return e}function ye(s){const e=typeof s=="string"?s.trim():"",i=e.indexOf("@"),t=e.slice(i+1);if(i<1||/\s/.test(e)||!t.includes(".")||t.startsWith(".")||t.endsWith("."))throw new v("Enter a valid email address");return _(e)}function te(s){if(s===void 0)return"member";if(s!=="owner"&&s!=="member")throw new v("Unknown role");return s}export{Te as ApigeeService,S as ForbiddenError,b as NotFoundError,v as ValidationError};
|
|
1
|
+
var u=function(s,e,i,t){function r(o){return o instanceof i?o:new i(function(n){n(o)})}return new(i||(i=Promise))(function(o,n){function a(p){try{l(t.next(p))}catch(c){n(c)}}function d(p){try{l(t.throw(p))}catch(c){n(c)}}function l(p){p.done?o(p.value):r(p.value).then(a,d)}l((t=t.apply(s,e||[])).next())})},I=function(s,e,i,t){if(i==="a"&&!t)throw new TypeError("Private accessor was defined without a getter");if(typeof e=="function"?s!==e||!t:!e.has(s))throw new TypeError("Cannot read private member from an object whose class did not declare it");return i==="m"?t:i==="a"?t.call(s):t?t.value:e.get(s)},$=function(s,e,i,t,r){if(t==="m")throw new TypeError("Private method is not writable");if(t==="a"&&!r)throw new TypeError("Private accessor was defined without a setter");if(typeof e=="function"?s!==e||!r:!e.has(s))throw new TypeError("Cannot write private member to an object whose class did not declare it");return t==="a"?r.call(s,i):r?r.value=i:e.set(s,i),i},D=function(s,e){var i={};for(var t in s)Object.prototype.hasOwnProperty.call(s,t)&&e.indexOf(t)<0&&(i[t]=s[t]);if(s!=null&&typeof Object.getOwnPropertySymbols=="function")for(var r=0,t=Object.getOwnPropertySymbols(s);r<t.length;r++)e.indexOf(t[r])<0&&Object.prototype.propertyIsEnumerable.call(s,t[r])&&(i[t[r]]=s[t[r]]);return i},R,j;import{createHash as ne,randomBytes as oe}from"node:crypto";import{isApiProductOffered as C}from"../config.js";import{APIGEE_DEFAULT_KEY_LIFETIME_SECONDS as L,APIGEE_DISPLAY_NAME_ATTRIBUTE as x,APIGEE_NOTES_ATTRIBUTE as k,APIGEE_PRODUCTS_ENTITY_KEY as se,APIGEE_PRODUCTS_VALUE_KEY as ae,APIGEE_TEAM_MEMBERS_ATTRIBUTE as F}from"../constants.js";import{maskKey as V}from"../contract.js";import{ApigeeApiError as de}from"./client.js";import{attributesToRecord as O,recordToAttributes as K,toApiProduct as z,toApp as S,toCredential as Y}from"./mappers.js";import{ApigeeStore as le,normalizeEmail as _}from"./store.js";class y extends Error{constructor(){super(...arguments),Object.defineProperty(this,"name",{enumerable:!0,configurable:!0,writable:!0,value:"ValidationError"})}}class q extends Error{constructor(){super(...arguments),Object.defineProperty(this,"name",{enumerable:!0,configurable:!0,writable:!0,value:"ForbiddenError"})}}class b extends Error{constructor(){super(...arguments),Object.defineProperty(this,"name",{enumerable:!0,configurable:!0,writable:!0,value:"NotFoundError"})}}const ce={byApiId:{},byTitle:{}},E={none:0,read:1,triage:2,write:3,maintain:4,admin:5};function T(s){return E[s]>=E.triage}function M(s){return E[s]>=E.maintain}function ue(s,e){return E[s]>=E[e]?s:e}const pe=/^[A-Za-z0-9._$ -]{1,100}$/,me=new Set(["teams","admin"]),he=/^[a-z0-9][a-z0-9-]{1,62}$/,P=2e3,fe=2048;function ve(s){return Buffer.from(JSON.stringify(s)).toString("base64url")}function H(s){return ne("sha256").update(s).digest("base64url")}function ye(s){try{const e=JSON.parse(Buffer.from(s,"base64url").toString());if(!e||typeof e.appId!="string"||typeof e.keyDigest!="string"||typeof e.apiProduct!="string"||!e.owner)throw new Error("shape");return e}catch{throw new y("Unknown request")}}function we(s){return s.trim().toLowerCase().split(/[^a-z0-9]/).filter(Boolean).join("-").slice(0,63)}function ge(s){var e,i,t,r;const o=((i=s.name)!==null&&i!==void 0?i:"").trim().split(/\s+/).filter(Boolean),n=(r=(e=((t=s.email)!==null&&t!==void 0?t:"").split("@")[0])===null||e===void 0?void 0:e.split(/[.-]/))!==null&&r!==void 0?r:[];return{firstName:o[0]||n[0]||"Unknown",lastName:o.slice(1).join(" ")||n[1]||"Unknown"}}function J(s){return oe(s).toString("base64url")}function W(s,e){const i=O(s.attributes)[F];if(!i)return[];try{const t=JSON.parse(i);return Array.isArray(t)?t.flatMap(r=>{var o,n;if(typeof r?.developer!="string")return[];const a=_(r.developer),d=e?.members.find(l=>l.email===a);return[{email:a,role:!((o=r.roles)===null||o===void 0)&&o.includes("owner")?"owner":"member",addedAt:(n=d?.addedAt)!==null&&n!==void 0?n:Number(s.createdAt)||0}]}):[]}catch{return[]}}function X(s,e){var i,t,r,o;const n=W(s,e);return{id:s.name,name:s.displayName||s.name,description:(t=O(s.attributes)[k])!==null&&t!==void 0?t:"",createdAt:Number(s.createdAt)||e?.createdAt||Date.now(),createdBy:(o=(r=e?.createdBy)!==null&&r!==void 0?r:(i=n.find(a=>a.role==="owner"))===null||i===void 0?void 0:i.email)!==null&&o!==void 0?o:"",members:n}}function Z(s){return JSON.stringify(s.map(e=>({developer:e.email,roles:[e.role]})))}class Re{constructor(e,i,t,r,o){Object.defineProperty(this,"settings",{enumerable:!0,configurable:!0,writable:!0,value:e}),Object.defineProperty(this,"client",{enumerable:!0,configurable:!0,writable:!0,value:i}),Object.defineProperty(this,"viewer",{enumerable:!0,configurable:!0,writable:!0,value:t}),Object.defineProperty(this,"ctx",{enumerable:!0,configurable:!0,writable:!0,value:r}),Object.defineProperty(this,"logger",{enumerable:!0,configurable:!0,writable:!0,value:o}),Object.defineProperty(this,"store",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),R.set(this,void 0),j.set(this,void 0),this.store=new le(r.kv)}get email(){return _(this.viewer.email)}apigeeProducts(){var e;return $(this,R,(e=I(this,R,"f"))!==null&&e!==void 0?e:this.client.listApiProducts(),"f"),I(this,R,"f")}productPages(){var e;return $(this,j,(e=I(this,j,"f"))!==null&&e!==void 0?e:this.ctx.entities.load(se).then(i=>{var t;return(t=i?.[ae])!==null&&t!==void 0?t:ce}),"f"),I(this,j,"f")}pageFor(e,i){const t=i.byApiId[e.name];if(t||!this.settings.apiProducts.matchByTitle)return t;const r=(e.displayName||e.name).trim().toLowerCase();return i.byTitle[r]}roleFor(e,i,t){const r=t.find(a=>a.name===e),o=r?this.pageFor(r,i):i.byApiId[e],n=o?this.ctx.roleForSlug(o.slug):"none";return ue(n,this.ctx.roleForSlug(this.settings.basePath))}approvalSummary(){return u(this,void 0,void 0,function*(){if(!this.settings.approvalsEnabled)return{enabled:!1,canView:!1,canDecide:!1};const e=this.ctx.roleForSlug(this.settings.basePath);if(M(e))return{enabled:!0,canView:!0,canDecide:!0};const[i,t]=yield Promise.all([this.apigeeProducts(),this.productPages()]);let r=T(e),o=!1;for(const n of i){if(!C(this.settings,n.name))continue;const a=this.roleFor(n.name,t,i);r||(r=T(a)),o||(o=M(a))}return{enabled:!0,canView:r,canDecide:o}})}canReview(){return u(this,void 0,void 0,function*(){return(yield this.approvalSummary()).canView})}listApiProducts(){return u(this,void 0,void 0,function*(){const[e,i]=yield Promise.all([this.apigeeProducts(),this.productPages()]),t=[];for(const r of e){if(!C(this.settings,r.name))continue;const o=this.pageFor(r,i);if(o){if(!this.ctx.isSlugVisible(o.slug))continue;t.push(z(r,o))}else this.settings.apiProducts.allowOutsideCatalog&&t.push(z(r))}return t.sort((r,o)=>r.name.localeCompare(o.name))})}assertProductsAllowed(e){return u(this,void 0,void 0,function*(){const i=ee(e),t=yield this.listApiProducts(),r=i.filter(n=>!t.some(a=>a.id===n));if(r.length>0)throw new y(`Unknown API product: ${r.join(", ")}`);const o=new Map(t.map(n=>[n.id,n]));return[...new Set(i)].flatMap(n=>{const a=o.get(n);return a?[a]:[]})})}resolveOwner(e){return u(this,void 0,void 0,function*(){if(!e)return{owner:{kind:"developer",email:this.email},path:{kind:"developer",email:this.email}};const i=yield this.requireTeam(e);return{owner:{kind:"team",teamId:i.id},path:{kind:"appgroup",name:i.id}}})}ownerPath(e){return e.kind==="developer"?{kind:"developer",email:e.email}:{kind:"appgroup",name:e.teamId}}withDecisions(e){return u(this,void 0,void 0,function*(){const i=[];for(const o of e)for(const n of o.credentials)for(const a of n.apiProducts)i.push({owner:o.owner,appId:o.id,consumerKey:n.consumerKey,apiProduct:a.id});const t=yield this.store.getDecisions(i);let r=0;for(const o of e)for(const n of o.credentials)for(const a of n.apiProducts){const d=t[r++];d&&(a.decision=d)}return e})}listApps(){return u(this,void 0,void 0,function*(){return this.appsOf(yield this.listMyTeams())})}appsOf(e){return u(this,void 0,void 0,function*(){const[i,...t]=yield Promise.all([this.client.listApps({kind:"developer",email:this.email}),...e.map(o=>this.client.listApps({kind:"appgroup",name:o.id}))]),r=i.map(o=>S(o,{kind:"developer",email:this.email}));return e.forEach((o,n)=>{r.push(...t[n].map(a=>S(a,{kind:"team",teamId:o.id})))}),this.withDecisions(r.sort((o,n)=>{var a,d;return((a=n.createdAt)!==null&&a!==void 0?a:0)-((d=o.createdAt)!==null&&d!==void 0?d:0)}))})}createApp(e){return u(this,void 0,void 0,function*(){var i;const t=te(e.name),r=A(e.description,P,"description"),o=ie(e.callbackUrl),n=A(e.note,P,"note"),a=yield this.assertProductsAllowed((i=e.apiProducts)!==null&&i!==void 0?i:[]),{owner:d,path:l}=yield this.resolveOwner(e.teamId);if(d.kind==="developer"&&(yield this.client.ensureDeveloper(Object.assign({email:this.email,userName:this.email},ge(this.viewer)))),yield this.client.getApp(l,t))throw new y(`An app named "${t}" already exists`);const p=yield this.client.createApp(l,Object.assign(Object.assign({name:t,apiProducts:a.map(m=>m.id)},o?{callbackUrl:o}:{}),{keyExpiresIn:String(L*(d.kind==="developer"?1e3:1)),attributes:K({[x]:t,[k]:r??""})})),c=S(p,d);return yield this.recordRequestNotes(d,c.id,c.credentials,a,n),c})}updateApp(e,i,t){return u(this,void 0,void 0,function*(){var r;const{owner:o,path:n}=yield this.resolveOwner(e),a=yield this.client.getApp(n,i);if(!a)throw new b("App not found");const d=O(a.attributes);t.name!==void 0&&(d[x]=te(t.name)),t.description!==void 0&&(d[k]=(r=A(t.description,P,"description"))!==null&&r!==void 0?r:"");const l=t.callbackUrl===void 0?a.callbackUrl:ie(t.callbackUrl),p=yield this.client.updateApp(n,i,Object.assign({attributes:K(d)},l!==void 0?{callbackUrl:l}:{}));return S(p,o)})}deleteApp(e,i){return u(this,void 0,void 0,function*(){const{path:t}=yield this.resolveOwner(e);if(!(yield this.client.getApp(t,i)))throw new b("App not found");yield this.client.deleteApp(t,i)})}createKey(e,i,t){return u(this,void 0,void 0,function*(){var r;const{owner:o,path:n}=yield this.resolveOwner(e);if(!(yield this.client.getApp(n,i)))throw new b("App not found");const a=yield this.assertProductsAllowed((r=t.apiProducts)!==null&&r!==void 0?r:[]),d=A(t.note,P,"note"),l=yield this.client.createKey(n,i,{consumerKey:J(24),consumerSecret:J(24),expiresInSeconds:L}),p=a.length>0?yield this.client.addKeyProducts(n,i,l.consumerKey,a.map(m=>m.id),l.attributes):l,c=Y(p);return yield this.recordRequestNotes(o,i,[c],a,d),c})}updateKeyProducts(e,i,t,r){return u(this,void 0,void 0,function*(){var o,n;const{owner:a,path:d}=yield this.resolveOwner(e),l=yield this.client.getKey(d,i,t);if(!l)throw new b("Key not found");const p=new Set(((o=l.apiProducts)!==null&&o!==void 0?o:[]).map(g=>g.apiproduct)),c=ee((n=r.apiProducts)!==null&&n!==void 0?n:[]),m=yield this.assertProductsAllowed(c.filter(g=>!p.has(g))),w=A(r.note,P,"note"),h=[...p].filter(g=>!c.includes(g));let f=l;m.length>0&&(f=yield this.client.addKeyProducts(d,i,t,m.map(g=>g.id),l.attributes));for(const g of h)yield this.client.removeKeyProduct(d,i,t,g),yield this.store.deleteRequestNote({owner:a,appId:i,consumerKey:t,apiProduct:g}),yield this.store.deleteDecision({owner:a,appId:i,consumerKey:t,apiProduct:g});const v=h.length>0?yield this.client.getKey(d,i,t):null,N=Y(v??f);return yield this.recordRequestNotes(a,i,[N],m,w),N})}deleteKey(e,i,t){return u(this,void 0,void 0,function*(){const{path:r}=yield this.resolveOwner(e);if(!(yield this.client.getKey(r,i,t)))throw new b("Key not found");yield this.client.deleteKey(r,i,t)})}recordRequestNotes(e,i,t,r,o){return u(this,void 0,void 0,function*(){const n=r.filter(d=>d.approvalType==="manual");if(n.length===0)return;const a=Date.now();for(const d of t)for(const l of n)yield this.store.putRequestNote({owner:e,appId:i,consumerKey:d.consumerKey,apiProduct:l.id},Object.assign({requestedBy:this.email,requestedAt:a},o?{note:o}:{}))})}getAccess(e){return u(this,void 0,void 0,function*(){const i=yield this.appsOf(yield this.listMyTeams()),t=Date.now(),r=n=>i.flatMap(a=>a.credentials.filter(d=>a.status!=="revoked"&&d.status==="approved"&&(d.expiresAt===null||d.expiresAt>t)&&d.apiProducts.some(l=>l.id===e&&l.status===n)).map(d=>({app:a,credential:d}))),o=r("approved").sort((n,a)=>n.app.owner.kind===a.app.owner.kind?0:n.app.owner.kind==="developer"?-1:1);if(o.length>0){const{credential:n}=o[0];return{status:"APPROVED",link:this.settings.basePath,requestValues:{security:{default:Object.assign({client_id:n.consumerKey},n.consumerSecret?{client_secret:n.consumerSecret}:{})}}}}return{status:r("pending").length>0?"PENDING":"NONE",link:`${this.settings.basePath}?createApp&apiProduct=${encodeURIComponent(e)}`}})}assertTeams(){if(!this.settings.teamsEnabled)throw new q("Teams are disabled")}withRole(e){const i=e.members.find(t=>t.email===this.email);return i?Object.assign(Object.assign({},e),{myRole:i.role}):e}listMyTeams(){return u(this,void 0,void 0,function*(){var e,i;if(!this.settings.teamsEnabled)return[];let t;try{t=yield this.client.listAppGroups()}catch(n){return(e=this.logger)===null||e===void 0||e.debug(`apigee: AppGroups unavailable: ${String(n)}`),[]}const r=new Map((yield this.store.listTeamsOf(this.email)).map(n=>[n.id,n])),o=[];for(const n of t){if(!W(n).some(d=>d.email===this.email))continue;const a=X(n,(i=r.get(n.name))!==null&&i!==void 0?i:yield this.store.getTeam(n.name));r.has(a.id)||(yield this.store.putTeam(a)),o.push(this.withRole(a))}return o.sort((n,a)=>n.name.localeCompare(a.name))})}requireTeam(e,i){return u(this,void 0,void 0,function*(){this.assertTeams();const[t,r]=yield Promise.all([this.client.getAppGroup(e),this.store.getTeam(e)]);if(!t)throw new b("Team not found");const o=this.withRole(X(t,r));if(!o.myRole)throw new q("You are not a member of this team");if(i==="owner"&&o.myRole!=="owner")throw new q("Only a team owner can do this");return o})}getTeam(e){return u(this,void 0,void 0,function*(){return this.requireTeam(e)})}createTeam(e){return u(this,void 0,void 0,function*(){var i;this.assertTeams();const t=A(e.name,100,"name");if(!t)throw new y("A team needs a name");const r=we(t);if(!he.test(r))throw new y("The team name needs at least two letters or digits");const o=(i=A(e.description,P,"description"))!==null&&i!==void 0?i:"";if((yield this.store.getTeam(r))||(yield this.client.getAppGroup(r)))throw new y(`A team named "${t}" already exists`);const n=[{email:this.email,role:"owner",addedAt:Date.now()}];yield this.client.createAppGroup({name:r,displayName:t,attributes:K({[k]:o,[F]:Z(n)})});const a={id:r,name:t,description:o,createdAt:Date.now(),createdBy:this.email,members:n};return yield this.store.putTeam(a),this.withRole(a)})}updateTeam(e,i){return u(this,void 0,void 0,function*(){var t;const r=yield this.requireTeam(e,"owner"),o=A(i.name,100,"name")||r.name,n=(t=A(i.description,P,"description"))!==null&&t!==void 0?t:r.description,a=Object.assign(Object.assign({},G(r)),{name:o,description:n});return yield this.syncAppGroup(a),yield this.store.putTeam(a),this.withRole(a)})}deleteTeam(e){return u(this,void 0,void 0,function*(){const i=yield this.requireTeam(e,"owner"),t={kind:"appgroup",name:i.id};for(const r of yield this.client.listApps(t))yield this.client.deleteApp(t,r.name);try{yield this.client.deleteAppGroup(i.id)}catch(r){if(!(r instanceof de&&r.status===404))throw r}yield this.store.deleteTeam(i.id)})}addMember(e,i){return u(this,void 0,void 0,function*(){const t=yield this.requireTeam(e,"owner"),r=be(i.email),o=re(i.role);if(t.members.some(a=>a.email===r))throw new y(`${r} is already a member`);const n=Object.assign(Object.assign({},G(t)),{members:[...t.members,{email:r,role:o,addedAt:Date.now()}]});return yield this.syncAppGroup(n),yield this.store.putTeam(n),this.withRole(n)})}setMemberRole(e,i,t){return u(this,void 0,void 0,function*(){const r=yield this.requireTeam(e,"owner"),o=_(i);if(!r.members.some(d=>d.email===o))throw new b("Member not found");const n=r.members.map(d=>d.email===o?Object.assign(Object.assign({},d),{role:re(t)}):d);Q(n);const a=Object.assign(Object.assign({},G(r)),{members:n});return yield this.syncAppGroup(a),yield this.store.putTeam(a),this.withRole(a)})}removeMember(e,i){return u(this,void 0,void 0,function*(){const t=yield this.requireTeam(e),r=_(i);if(t.myRole!=="owner"&&r!==this.email)throw new q("Only a team owner can remove other members");if(!t.members.some(a=>a.email===r))throw new b("Member not found");const o=t.members.filter(a=>a.email!==r);Q(o);const n=Object.assign(Object.assign({},G(t)),{members:o});return yield this.syncAppGroup(n),yield this.store.putTeam(n),this.withRole(n)})}syncAppGroup(e){return u(this,void 0,void 0,function*(){const i=yield this.client.getAppGroup(e.id);if(!i)throw new b("Team not found");const t=Object.assign(Object.assign({},O(i.attributes)),{[k]:e.description,[F]:Z(e.members)});yield this.client.updateAppGroup(e.id,Object.assign(Object.assign({},i),{name:e.id,displayName:e.name,attributes:K(t)}))})}ownerOf(e,i){return u(this,void 0,void 0,function*(){var t;if(e.appGroup)return{kind:"team",teamId:e.appGroup};if(e.developerEmail)return{kind:"developer",email:_(e.developerEmail)};if(e.developerId){let r=i.get(e.developerId);if(r||(r=(t=yield this.client.getDeveloper(e.developerId))===null||t===void 0?void 0:t.email,r&&i.set(e.developerId,r)),r)return{kind:"developer",email:_(r)}}return null})}teamNames(){return u(this,void 0,void 0,function*(){var e;const i=new Map;if(!this.settings.teamsEnabled)return i;let t;try{t=yield this.client.listAppGroups()}catch(r){return(e=this.logger)===null||e===void 0||e.debug(`apigee: AppGroups unavailable: ${String(r)}`),i}for(const r of t)r.displayName&&i.set(r.name,r.displayName);return i})}listRequests(){return u(this,void 0,void 0,function*(){var e,i;const[t,r,o,n]=yield Promise.all([this.client.listOrganizationApps(),this.apigeeProducts(),this.productPages(),this.teamNames()]),a=new Map,d=[];for(const l of t){const p=((e=l.credentials)!==null&&e!==void 0?e:[]).flatMap(w=>{var h;return((h=w.apiProducts)!==null&&h!==void 0?h:[]).filter(f=>f.status==="pending").filter(f=>T(this.roleFor(f.apiproduct,o,r))).map(f=>({credential:w,apiProduct:f.apiproduct}))});if(p.length===0)continue;const c=yield this.ownerOf(l,a);if(!c)continue;const m=O(l.attributes)[x];for(const{credential:w,apiProduct:h}of p){const{consumerKey:f}=w,v=yield this.store.getRequestNote({owner:c,appId:l.name,consumerKey:f,apiProduct:h});d.push(Object.assign(Object.assign(Object.assign(Object.assign({id:ve({owner:c,appId:l.name,keyDigest:H(f),apiProduct:h}),owner:c},v?{requestedBy:v.requestedBy}:{}),{teamName:U(c,n),appId:l.name,appName:m||l.name,consumerKey:V(f),apiProduct:h,apiProductName:B(r,h)}),v?.note?{note:v.note}:{}),{requestedAt:(i=v?.requestedAt)!==null&&i!==void 0?i:Number(w.issuedAt)||null,canDecide:M(this.roleFor(h,o,r))}))}}return d.sort((l,p)=>{var c,m;return((c=p.requestedAt)!==null&&c!==void 0?c:0)-((m=l.requestedAt)!==null&&m!==void 0?m:0)})})}decideRequest(e,i,t){return u(this,void 0,void 0,function*(){var r;const o=ye(e),{keyDigest:n}=o,a=D(o,["keyDigest"]),[d,l]=yield Promise.all([this.apigeeProducts(),this.productPages()]);if(!M(this.roleFor(a.apiProduct,l,d)))throw new q("You cannot decide requests for this API product");const p=A(t,P,"note"),c=this.ownerPath(a.owner),m=yield this.client.getApp(c,a.appId);if(!m)throw new b("App not found");const w=(r=m.credentials)===null||r===void 0?void 0:r.find(v=>H(v.consumerKey)===n);if(!w)throw new b("Key not found");const h=Object.assign(Object.assign({},a),{consumerKey:w.consumerKey});yield this.client.setKeyProductStatus(c,h.appId,h.consumerKey,h.apiProduct,i),yield this.store.deleteRequestNote(h);const f=Object.assign({action:i,actor:this.email,at:Date.now()},p?{note:p}:{});return yield this.store.putDecision(h,f),this.store.appendAudit(Object.assign({at:f.at,actor:this.email,action:i,owner:h.owner,appId:h.appId,appName:O(m.attributes)[x]||m.name,consumerKey:V(h.consumerKey),apiProduct:h.apiProduct},p?{note:p}:{}))})}listAllApps(){return u(this,void 0,void 0,function*(){var e;const[i,t,r,o]=yield Promise.all([this.client.listOrganizationApps(),this.apigeeProducts(),this.productPages(),this.teamNames()]),n=T(this.ctx.roleForSlug(this.settings.basePath)),a=new Map,d=[];for(const l of i){if(!(n||((e=l.credentials)!==null&&e!==void 0?e:[]).some(f=>{var v;return((v=f.apiProducts)!==null&&v!==void 0?v:[]).some(N=>T(this.roleFor(N.apiproduct,r,t)))})))continue;const c=yield this.ownerOf(l,a);if(!c)continue;const m=S(l,c),{credentials:w}=m,h=D(m,["credentials"]);for(const f of w.flatMap(v=>v.apiProducts))f.name=B(t,f.id);d.push(Object.assign(Object.assign({},h),{teamName:U(c,o),credentials:w.map(f=>{var{consumerKey:v,consumerSecret:N}=f,g=D(f,["consumerKey","consumerSecret"]);return g})}))}return d.sort((l,p)=>{var c,m;return((c=p.createdAt)!==null&&c!==void 0?c:0)-((m=l.createdAt)!==null&&m!==void 0?m:0)})})}listAudit(){return u(this,void 0,void 0,function*(){const[e,i,t,r]=yield Promise.all([this.store.listAudit(),this.apigeeProducts(),this.productPages(),this.teamNames()]),o=e.filter(n=>T(this.roleFor(n.apiProduct,t,i)));for(const n of o)n.teamName=U(n.owner,r),n.apiProductName=B(i,n.apiProduct);return o})}}R=new WeakMap,j=new WeakMap;function U(s,e){return s.kind==="team"?e.get(s.teamId):void 0}function B(s,e){var i;return(i=s.find(t=>t.name===e))===null||i===void 0?void 0:i.displayName}function G(s){const{myRole:e}=s;return D(s,["myRole"])}function Q(s){if(!s.some(e=>e.role==="owner"))throw new y("A team needs at least one owner")}function ee(s){if(!Array.isArray(s)||s.some(e=>typeof e!="string"))throw new y("apiProducts must be a list of product names");return s}function te(s){if(typeof s!="string"||!pe.test(s.trim()))throw new y("An app name has 1 to 100 letters, digits, spaces, dots, dashes or underscores");const e=s.trim();if(me.has(e))throw new y(`An app cannot be named "${e}"`);return e}function A(s,e,i){if(s==null)return;if(typeof s!="string")throw new y(`${i} must be text`);const t=s.trim();if(t.length>e)throw new y(`${i} is longer than ${e} characters`);return t}function ie(s){const e=A(s,fe,"callbackUrl");if(!e)return e===""?"":void 0;for(const i of e.split(","))try{new URL(i.trim())}catch{throw new y(`"${i.trim()}" is not a valid callback URL`)}return e}function be(s){const e=typeof s=="string"?s.trim():"",i=e.indexOf("@"),t=e.slice(i+1);if(i<1||/\s/.test(e)||!t.includes(".")||t.startsWith(".")||t.endsWith("."))throw new y("Enter a valid email address");return _(e)}function re(s){if(s===void 0)return"member";if(s!=="owner"&&s!=="member")throw new y("Unknown role");return s}export{Re as ApigeeService,q as ForbiddenError,b as NotFoundError,y as ValidationError};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@redocly/framework-plugin-apigee",
|
|
3
|
-
"version": "0.1.0-next.
|
|
3
|
+
"version": "0.1.0-next.10",
|
|
4
4
|
"description": "Experimental Apigee developer onboarding plugin for Redocly",
|
|
5
5
|
"author": "team@redocly.com",
|
|
6
6
|
"license": "SEE LICENSE IN LICENSE",
|
|
@@ -21,24 +21,25 @@
|
|
|
21
21
|
"apigee"
|
|
22
22
|
],
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@redocly/config": "0.
|
|
24
|
+
"@redocly/config": "0.75.0",
|
|
25
|
+
"@tanstack/react-query": "5.62.3",
|
|
25
26
|
"styled-components": "^6.4.2"
|
|
26
27
|
},
|
|
27
28
|
"devDependencies": {
|
|
28
|
-
"@redocly/framework": "1.0.0-next.
|
|
29
|
+
"@redocly/framework": "1.0.0-next.17",
|
|
29
30
|
"@types/node": "24.1.0",
|
|
30
31
|
"@types/react": "^19.2.7",
|
|
31
32
|
"@types/react-dom": "^19.2.3",
|
|
32
33
|
"@vitest/coverage-v8": "4.1.11",
|
|
33
34
|
"esbuild-minify-dir": "0.0.1",
|
|
34
|
-
"hono": "4.13.
|
|
35
|
+
"hono": "4.13.18",
|
|
35
36
|
"rimraf": "5.0.7",
|
|
36
37
|
"typescript": "6.0.3",
|
|
37
38
|
"vitest": "4.1.11"
|
|
38
39
|
},
|
|
39
40
|
"peerDependencies": {
|
|
40
|
-
"@redocly/framework": "^1.0.0-next.
|
|
41
|
-
"@redocly/theme": "0.73.0-next.
|
|
41
|
+
"@redocly/framework": "^1.0.0-next.17",
|
|
42
|
+
"@redocly/theme": "0.73.0-next.12",
|
|
42
43
|
"react": "^19.2.7",
|
|
43
44
|
"react-dom": "^19.2.7",
|
|
44
45
|
"react-router": "^8.3.0"
|