-
Notifications
You must be signed in to change notification settings - Fork 13.1k
chore: add validation layer to marketplace comms #35059
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kodiakhq
merged 10 commits into
develop
from
chore/add-wrapper-for-fetching-marketplace-apps
Feb 10, 2025
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3140840
chore: add validation layer to marketplace comms
Gustrb 29eb754
chore: reuse the already created method
Gustrb 3184bc5
Merge branch 'develop' into chore/add-wrapper-for-fetching-marketplac…
Gustrb 45f26c0
chore: add default params to improve devx
Gustrb 40b22fb
Merge branch 'develop' into chore/add-wrapper-for-fetching-marketplac…
Gustrb 249ccc8
chore: apply suggestions
Gustrb fca96e8
chore: rename var
Gustrb 0657dab
Merge branch 'develop' into chore/add-wrapper-for-fetching-marketplac…
Gustrb 562058e
Merge branch 'develop' into chore/add-wrapper-for-fetching-marketplac…
Gustrb 43fc9c2
Merge branch 'develop' into chore/add-wrapper-for-fetching-marketplac…
kodiakhq[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
180 changes: 180 additions & 0 deletions
180
apps/meteor/ee/server/apps/marketplace/fetchMarketplaceApps.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| import type { App } from '@rocket.chat/core-typings'; | ||
| import { serverFetch as fetch } from '@rocket.chat/server-fetch'; | ||
| import { v, compile } from 'suretype'; | ||
|
|
||
| import { getMarketplaceHeaders } from './getMarketplaceHeaders'; | ||
| import { getWorkspaceAccessToken } from '../../../../app/cloud/server'; | ||
| import { Apps } from '../orchestrator'; | ||
| import { MarketplaceAppsError, MarketplaceConnectionError } from './marketplaceErrors'; | ||
|
|
||
| type FetchMarketplaceAppsParams = { | ||
| endUserID?: string; | ||
| }; | ||
|
|
||
| const markdownObject = { | ||
| raw: v.string(), | ||
| rendered: v.string(), | ||
| }; | ||
|
|
||
| const fetchMarketplaceAppsSchema = v.array( | ||
| v.object({ | ||
| appId: v.string().required(), | ||
| latest: v | ||
| .object({ | ||
| internalId: v.string(), | ||
| id: v.string().required(), | ||
| name: v.string().required(), | ||
| nameSlug: v.string().required(), | ||
| version: v.string().required(), | ||
| categories: v.array(v.string()).required(), | ||
| languages: v.array(v.string()), | ||
| shortDescription: v.string(), | ||
| description: v.string().required(), | ||
| privacyPolicySummary: v.string(), | ||
| documentationUrl: v.string(), | ||
| detailedDescription: v.object(markdownObject).required(), | ||
| detailedChangelog: v.object(markdownObject).required(), | ||
|
|
||
| requiredApiVersion: v.string().required(), | ||
| versionIncompatible: v.boolean(), | ||
|
|
||
| permissions: v.array( | ||
| v.object({ | ||
| name: v.string().required(), | ||
| domains: v.array(v.string()), | ||
| scopes: v.array(v.string()), | ||
| }), | ||
| ), | ||
| addon: v.string(), | ||
| author: v | ||
| .object({ | ||
| name: v.string().required(), | ||
| support: v.string().required(), | ||
| homepage: v.string().required(), | ||
| }) | ||
| .required(), | ||
| classFile: v.string().required(), | ||
| iconFile: v.string().required(), | ||
| iconFileData: v.string().required(), | ||
| status: v.string().enum('submitted', 'author-rejected', 'author-approved', 'rejected', 'approved').required(), | ||
| reviewedNote: v.string(), | ||
| rejectionNote: v.string(), | ||
| changesNote: v.string(), | ||
| internalChangesNote: v.string(), | ||
| isVisible: v.boolean().required(), | ||
| createdDate: v.string().required(), | ||
| modifiedDate: v.string().required(), | ||
| }) | ||
| .required(), | ||
| isAddon: v.boolean().required(), | ||
| addonId: v.string(), | ||
| isEnterpriseOnly: v.boolean().required(), | ||
| isBundle: v.boolean().required(), | ||
| bundedAppIds: v.array(v.string()).required(), | ||
| bundledIn: v | ||
| .array( | ||
| v.object({ | ||
| bundleId: v.string(), | ||
| bundleName: v.string(), | ||
| addonTierId: v.string(), | ||
| }), | ||
| ) | ||
| .required(), | ||
| isPurchased: v.boolean().required(), | ||
| isSubscribed: v.boolean().required(), | ||
| subscriptionInfo: v.object({ | ||
| typeOf: v.string().enum('app', 'service').required(), | ||
| status: v.string().enum('trialing', 'active', 'cancelled', 'cancelling', 'pastDue').required(), | ||
| statusFromBilling: v.boolean().required(), | ||
| isSeatBased: v.boolean().required(), | ||
| seats: v.number().required(), | ||
| maxSeats: v.number().required(), | ||
| license: v | ||
| .object({ | ||
| license: v.string().required(), | ||
| version: v.number().required(), | ||
| expireDate: v.string().required(), | ||
| }) | ||
| .required(), | ||
| startDate: v.string().required(), | ||
| periodEnd: v.string().required(), | ||
| endDate: v.string(), | ||
| externallyManaged: v.boolean().required(), | ||
| isSubscribedViaBundle: v.boolean().required(), | ||
| }), | ||
| price: v.number().required(), | ||
| purchaseType: v.string().enum('', 'buy', 'subscription').required(), | ||
| pricingPlans: v.array( | ||
| v.object({ | ||
| id: v.string().required(), | ||
| enabled: v.boolean().required(), | ||
| price: v.number().required(), | ||
| trialDays: v.number().required(), | ||
| strategy: v.string().enum('once', 'monthly', 'yearly').required(), | ||
| isPerSeat: v.boolean().required(), | ||
| tiers: v.array( | ||
| v.object({ | ||
| perUnit: v.boolean().required(), | ||
| minimum: v.number().required(), | ||
| maximum: v.number().required(), | ||
| price: v.number().required(), | ||
| refId: v.string(), | ||
| }), | ||
| ), | ||
| }), | ||
| ), | ||
| isUsageBased: v.boolean().required(), | ||
|
|
||
| requestedEndUser: v.boolean(), | ||
| requested: v.boolean(), | ||
| appRequestStats: v.object({}), | ||
|
|
||
| createdAt: v.string().required(), | ||
| modifiedAt: v.string().required(), | ||
| }), | ||
| ); | ||
|
|
||
| const assertMarketplaceAppsSchema = compile(fetchMarketplaceAppsSchema); | ||
|
|
||
| export async function fetchMarketplaceApps({ endUserID }: FetchMarketplaceAppsParams = {}): Promise<App[]> { | ||
| const baseUrl = Apps.getMarketplaceUrl(); | ||
| const headers = getMarketplaceHeaders(); | ||
| const token = await getWorkspaceAccessToken(); | ||
| if (token) { | ||
| headers.Authorization = `Bearer ${token}`; | ||
| } | ||
|
|
||
| let request; | ||
| try { | ||
| request = await fetch(`${baseUrl}/v1/apps`, { | ||
| headers, | ||
| params: { | ||
| ...(endUserID && { endUserID }), | ||
| }, | ||
| }); | ||
| } catch (error) { | ||
| throw new MarketplaceConnectionError('Marketplace_Bad_Marketplace_Connection'); | ||
| } | ||
|
|
||
| if (request.status === 200) { | ||
| const response = await request.json(); | ||
| assertMarketplaceAppsSchema(response); | ||
| return response; | ||
| } | ||
|
|
||
| const response = await request.json(); | ||
|
|
||
| Apps.getRocketChatLogger().error('Failed to fetch marketplace apps', response); | ||
|
|
||
| if (request.status === 400 && response.code === 200) { | ||
| throw new MarketplaceAppsError('Marketplace_Invalid_Apps_Engine_Version'); | ||
| } | ||
|
|
||
| const INTERNAL_MARKETPLACE_ERROR_CODES = [266, 256, 166, 221, 257, 320]; | ||
|
|
||
| if (request.status === 500 && INTERNAL_MARKETPLACE_ERROR_CODES.includes(response.code)) { | ||
| throw new MarketplaceAppsError('Marketplace_Internal_Error'); | ||
| } | ||
|
|
||
| throw new MarketplaceAppsError('Marketplace_Failed_To_Fetch_Apps'); | ||
| } | ||
7 changes: 7 additions & 0 deletions
7
apps/meteor/ee/server/apps/marketplace/getMarketplaceHeaders.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import { Info } from '../../../../app/utils/rocketchat.info'; | ||
|
|
||
| export function getMarketplaceHeaders(): Record<string, any> { | ||
Gustrb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return { | ||
| 'X-Apps-Engine-Version': Info.marketplaceApiVersion.replace(/-.*/g, ''), | ||
| }; | ||
| } | ||
13 changes: 13 additions & 0 deletions
13
apps/meteor/ee/server/apps/marketplace/marketplaceErrors.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| export class MarketplaceAppsError extends Error { | ||
| constructor(message: string) { | ||
| super(message); | ||
| this.name = 'MarketplaceAppsError'; | ||
| } | ||
| } | ||
|
|
||
| export class MarketplaceConnectionError extends Error { | ||
| constructor(message: string) { | ||
| super(message); | ||
| this.name = 'MarketplaceConnectionError'; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.