mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-12 05:48:57 +00:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d9b5a898ab | |||
| f127d28675 | |||
| 65c1bdc455 | |||
| 0711a171d4 | |||
| c58c551f9e | |||
| f574858b9b | |||
| 3e1592e16b |
@@ -1,6 +1,12 @@
|
||||
name: 'Deploy cloudflare'
|
||||
description: 'Deploy GitBook to Cloudflare'
|
||||
inputs:
|
||||
opItem:
|
||||
description: '1Password item to load secrets from'
|
||||
required: true
|
||||
opServiceAccount:
|
||||
description: '1Password service account token'
|
||||
required: true
|
||||
apiToken:
|
||||
description: 'Cloudflare API token'
|
||||
required: true
|
||||
@@ -8,44 +14,11 @@ inputs:
|
||||
description: 'Cloudflare account ID'
|
||||
required: true
|
||||
environment:
|
||||
description: 'Environment to deploy to'
|
||||
description: 'Cloudflare environment to deploy to (staging, production, preview)'
|
||||
required: true
|
||||
deploy:
|
||||
description: 'Deploy as main version for all traffic instead of uploading versions'
|
||||
required: true
|
||||
NEXT_SERVER_ACTIONS_ENCRYPTION_KEY:
|
||||
description: 'Next server actions encryption key'
|
||||
required: true
|
||||
GITBOOK_URL:
|
||||
description: 'GitBook URL'
|
||||
required: true
|
||||
GITBOOK_SECRET:
|
||||
description: 'GitBook secret'
|
||||
required: true
|
||||
GITBOOK_APP_URL:
|
||||
description: 'GitBook app URL'
|
||||
required: false
|
||||
GITBOOK_API_URL:
|
||||
description: 'GitBook API URL'
|
||||
required: false
|
||||
GITBOOK_INTEGRATIONS_HOST:
|
||||
description: 'GitBook integrations host'
|
||||
required: false
|
||||
GITBOOK_IMAGE_RESIZE_SIGNING_KEY:
|
||||
description: 'GitBook image resize signing key'
|
||||
required: true
|
||||
GITBOOK_IMAGE_RESIZE_URL:
|
||||
description: 'GitBook image resize URL'
|
||||
required: true
|
||||
GITBOOK_ICONS_URL:
|
||||
description: 'GitBook icons URL'
|
||||
required: true
|
||||
GITBOOK_ICONS_TOKEN:
|
||||
description: 'GitBook icons token'
|
||||
required: true
|
||||
GITBOOK_ASSETS_PREFIX:
|
||||
description: 'GitBook assets prefix'
|
||||
required: false
|
||||
outputs:
|
||||
deployment-url:
|
||||
description: "Deployment URL"
|
||||
@@ -60,21 +33,25 @@ runs:
|
||||
shell: bash
|
||||
env:
|
||||
PUPPETEER_SKIP_DOWNLOAD: 1
|
||||
- name: Load secret
|
||||
uses: 1password/load-secrets-action@v2
|
||||
env:
|
||||
OP_SERVICE_ACCOUNT_TOKEN: ${{ inputs.opServiceAccount }}
|
||||
GITBOOK_URL: ${{ inputs.opItem }}/GITBOOK_URL
|
||||
GITBOOK_ICONS_URL: ${{ inputs.opItem }}/GITBOOK_ICONS_URL
|
||||
GITBOOK_ICONS_TOKEN: ${{ inputs.opItem }}/GITBOOK_ICONS_TOKEN
|
||||
NEXT_SERVER_ACTIONS_ENCRYPTION_KEY: ${{ inputs.opItem }}/NEXT_SERVER_ACTIONS_ENCRYPTION_KEY
|
||||
GITBOOK_SECRET: ${{ inputs.opItem }}/GITBOOK_SECRET
|
||||
GITBOOK_APP_URL: ${{ inputs.opItem }}/GITBOOK_APP_URL
|
||||
GITBOOK_API_URL: ${{ inputs.opItem }}/GITBOOK_API_URL
|
||||
GITBOOK_API_TOKEN: ${{ inputs.opItem }}/GITBOOK_API_TOKEN
|
||||
GITBOOK_INTEGRATIONS_HOST: ${{ inputs.opItem }}/GITBOOK_INTEGRATIONS_HOST
|
||||
GITBOOK_IMAGE_RESIZE_SIGNING_KEY: ${{ inputs.opItem }}/GITBOOK_IMAGE_RESIZE_SIGNING_KEY
|
||||
GITBOOK_IMAGE_RESIZE_URL: ${{ inputs.opItem }}/GITBOOK_IMAGE_RESIZE_URL
|
||||
GITBOOK_ASSETS_PREFIX: ${{ inputs.opItem }}/GITBOOK_ASSETS_PREFIX
|
||||
- name: Build worker
|
||||
run: bun run turbo build:v2:cloudflare
|
||||
shell: bash
|
||||
env:
|
||||
NEXT_SERVER_ACTIONS_ENCRYPTION_KEY: ${{ inputs.NEXT_SERVER_ACTIONS_ENCRYPTION_KEY }}
|
||||
GITBOOK_URL: ${{ inputs.GITBOOK_URL }}
|
||||
GITBOOK_SECRET: ${{ inputs.GITBOOK_SECRET }}
|
||||
GITBOOK_APP_URL: ${{ inputs.GITBOOK_APP_URL }}
|
||||
GITBOOK_API_URL: ${{ inputs.GITBOOK_API_URL }}
|
||||
GITBOOK_INTEGRATIONS_HOST: ${{ inputs.GITBOOK_INTEGRATIONS_HOST }}
|
||||
GITBOOK_IMAGE_RESIZE_SIGNING_KEY: ${{ inputs.GITBOOK_IMAGE_RESIZE_SIGNING_KEY }}
|
||||
GITBOOK_IMAGE_RESIZE_URL: ${{ inputs.GITBOOK_IMAGE_RESIZE_URL }}
|
||||
GITBOOK_ICONS_URL: ${{ inputs.GITBOOK_ICONS_URL }}
|
||||
GITBOOK_ICONS_TOKEN: ${{ inputs.GITBOOK_ICONS_TOKEN }}
|
||||
GITBOOK_ASSETS_PREFIX: ${{ inputs.GITBOOK_ASSETS_PREFIX }}
|
||||
- id: deploy
|
||||
name: Deploy to Cloudflare
|
||||
uses: cloudflare/wrangler-action@v3.14.0
|
||||
@@ -84,7 +61,7 @@ runs:
|
||||
workingDirectory: ./
|
||||
wranglerVersion: '3.112.0'
|
||||
environment: ${{ inputs.environment }}
|
||||
command: ${{ inputs.deploy && 'deploy' || 'versions upload' }} --config ./packages/gitbook-v2/wrangler.toml
|
||||
command: ${{ fromJSON(inputs.deploy) == true && 'deploy' || 'versions upload' }} --config ./packages/gitbook-v2/wrangler.toml
|
||||
- name: Outputs
|
||||
shell: bash
|
||||
env:
|
||||
|
||||
@@ -1,15 +1,21 @@
|
||||
name: 'Deploy vercel'
|
||||
description: 'Deploy GitBook to Vercel'
|
||||
inputs:
|
||||
vercel-org:
|
||||
vercelOrg:
|
||||
description: 'Vercel organization'
|
||||
required: true
|
||||
vercel-project:
|
||||
vercelProject:
|
||||
description: 'Vercel project'
|
||||
required: true
|
||||
vercel-token:
|
||||
vercelToken:
|
||||
description: 'Vercel token'
|
||||
required: true
|
||||
opItem:
|
||||
description: '1Password item to load secrets from'
|
||||
required: true
|
||||
opServiceAccount:
|
||||
description: '1Password service account token'
|
||||
required: true
|
||||
environment:
|
||||
description: 'Environment to deploy to'
|
||||
required: true
|
||||
@@ -27,31 +33,42 @@ runs:
|
||||
shell: bash
|
||||
env:
|
||||
PUPPETEER_SKIP_DOWNLOAD: 1
|
||||
- name: Sets env vars for environment
|
||||
shell: bash
|
||||
run: |
|
||||
echo "VERCEL_ENVIRONMENT=${{ inputs.environment }}" >> $GITHUB_ENV
|
||||
- name: Pull Vercel Environment Information
|
||||
run: bun run vercel pull --yes --environment=$VERCEL_ENVIRONMENT --token=${{ inputs.vercel-token }}
|
||||
run: bun run vercel pull --yes --environment=${{ inputs.environment }} --token=${{ inputs.vercelToken }}
|
||||
shell: bash
|
||||
env:
|
||||
VERCEL_ORG_ID: ${{ inputs.vercel-org }}
|
||||
VERCEL_PROJECT_ID: ${{ inputs.vercel-project }}
|
||||
VERCEL_ORG_ID: ${{ inputs.vercelOrg }}
|
||||
VERCEL_PROJECT_ID: ${{ inputs.vercelProject }}
|
||||
- name: Load secret
|
||||
uses: 1password/load-secrets-action@v2
|
||||
env:
|
||||
OP_SERVICE_ACCOUNT_TOKEN: ${{ inputs.opServiceAccount }}
|
||||
GITBOOK_URL: ${{ inputs.opItem }}/GITBOOK_URL
|
||||
GITBOOK_ICONS_URL: ${{ inputs.opItem }}/GITBOOK_ICONS_URL
|
||||
GITBOOK_ICONS_TOKEN: ${{ inputs.opItem }}/GITBOOK_ICONS_TOKEN
|
||||
GITBOOK_SECRET: ${{ inputs.opItem }}/GITBOOK_SECRET
|
||||
GITBOOK_APP_URL: ${{ inputs.opItem }}/GITBOOK_APP_URL
|
||||
GITBOOK_API_URL: ${{ inputs.opItem }}/GITBOOK_API_URL
|
||||
GITBOOK_API_TOKEN: ${{ inputs.opItem }}/GITBOOK_API_TOKEN
|
||||
GITBOOK_INTEGRATIONS_HOST: ${{ inputs.opItem }}/GITBOOK_INTEGRATIONS_HOST
|
||||
GITBOOK_IMAGE_RESIZE_SIGNING_KEY: ${{ inputs.opItem }}/GITBOOK_IMAGE_RESIZE_SIGNING_KEY
|
||||
GITBOOK_IMAGE_RESIZE_URL: ${{ inputs.opItem }}/GITBOOK_IMAGE_RESIZE_URL
|
||||
GITBOOK_ASSETS_PREFIX: ${{ inputs.opItem }}/GITBOOK_ASSETS_PREFIX
|
||||
- name: Build Project Artifacts
|
||||
run: bun run vercel build --target=$VERCEL_ENVIRONMENT --token=${{ inputs.vercel-token }}
|
||||
run: bun run vercel build --target=${{ inputs.environment }} --token=${{ inputs.vercelToken }}
|
||||
shell: bash
|
||||
env:
|
||||
VERCEL_ORG_ID: ${{ inputs.vercel-org }}
|
||||
VERCEL_PROJECT_ID: ${{ inputs.vercel-project }}
|
||||
VERCEL_ORG_ID: ${{ inputs.vercelOrg }}
|
||||
VERCEL_PROJECT_ID: ${{ inputs.vercelProject }}
|
||||
- name: Deploy Project Artifacts to Vercel
|
||||
id: deploy
|
||||
shell: bash
|
||||
run: |
|
||||
DEPLOYMENT_URL=$(bun run vercel deploy --prebuilt --target=$VERCEL_ENVIRONMENT --token=${{ inputs.vercel-token }})
|
||||
DEPLOYMENT_URL=$(bun run vercel deploy --prebuilt --target=${{ inputs.environment }} --token=${{ inputs.vercelToken }})
|
||||
echo "deployment-url=$DEPLOYMENT_URL" >> "$GITHUB_OUTPUT"
|
||||
env:
|
||||
VERCEL_ORG_ID: ${{ inputs.vercel-org }}
|
||||
VERCEL_PROJECT_ID: ${{ inputs.vercel-project }}
|
||||
VERCEL_ORG_ID: ${{ inputs.vercelOrg }}
|
||||
VERCEL_PROJECT_ID: ${{ inputs.vercelProject }}
|
||||
- name: Outputs
|
||||
shell: bash
|
||||
run: |
|
||||
|
||||
@@ -10,6 +10,9 @@ jobs:
|
||||
deploy-v1-cloudflare:
|
||||
name: Deploy v1 to Cloudflare Pages
|
||||
runs-on: ubuntu-latest
|
||||
environment:
|
||||
name: ${{ github.ref == 'refs/heads/main' && '1c-production' || '1c-preview' }}
|
||||
url: ${{ steps.deploy.outputs.deployment-url }}
|
||||
permissions:
|
||||
contents: read
|
||||
deployments: write
|
||||
@@ -63,56 +66,45 @@ jobs:
|
||||
deploy-v2-vercel:
|
||||
name: Deploy v2 to Vercel (preview)
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: read
|
||||
deployments: write
|
||||
issues: write
|
||||
pull-requests: write
|
||||
checks: write
|
||||
statuses: write
|
||||
environment:
|
||||
name: 2v-preview
|
||||
url: ${{ steps.deploy.outputs.deployment-url }}
|
||||
outputs:
|
||||
deployment-url: ${{ steps.deploy.outputs.deployment-url }}
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
- name: Deploy ${{ github.ref == 'refs/heads/main' && 'production' || 'preview' }}
|
||||
- name: Deploy to Vercel
|
||||
id: deploy
|
||||
uses: ./.github/composite/deploy-vercel
|
||||
with:
|
||||
environment: ${{ github.ref == 'refs/heads/main' && 'production' || 'preview' }}
|
||||
vercel-org: ${{ secrets.VERCEL_ORG_ID }}
|
||||
vercel-project: ${{ secrets.VERCEL_PROJECT_ID }}
|
||||
vercel-token: ${{ secrets.VERCEL_TOKEN }}
|
||||
environment: preview
|
||||
vercelOrg: ${{ secrets.VERCEL_ORG_ID }}
|
||||
vercelProject: ${{ secrets.VERCEL_PROJECT_ID }}
|
||||
vercelToken: ${{ secrets.VERCEL_TOKEN }}
|
||||
opItem: op://gitbook-open/2v-preview
|
||||
opServiceAccount: ${{ secrets.OP_SERVICE_ACCOUNT_TOKEN }}
|
||||
deploy-v2-cloudflare:
|
||||
name: Deploy v2 to Cloudflare Worker (preview)
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: read
|
||||
deployments: write
|
||||
issues: write
|
||||
pull-requests: write
|
||||
checks: write
|
||||
statuses: write
|
||||
environment:
|
||||
name: 2c-preview
|
||||
url: ${{ steps.deploy.outputs.deployment-url }}
|
||||
outputs:
|
||||
deployment-url: ${{ steps.deploy.outputs.deployment-url }}
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
- name: Deploy ${{ github.ref == 'refs/heads/main' && 'production' || 'preview' }}
|
||||
- name: Deploy to Cloudflare
|
||||
id: deploy
|
||||
uses: ./.github/composite/deploy-cloudflare
|
||||
with:
|
||||
environment: ${{ github.ref == 'refs/heads/main' && 'production' || 'preview' }}
|
||||
environment: preview
|
||||
deploy: ${{ github.ref == 'refs/heads/main' }}
|
||||
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
|
||||
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
|
||||
NEXT_SERVER_ACTIONS_ENCRYPTION_KEY: ${{ secrets.NEXT_SERVER_ACTIONS_ENCRYPTION_KEY }}
|
||||
GITBOOK_URL: ${{ github.ref == 'refs/heads/main' && vars.PRODUCTION_2C_GITBOOK_URL || vars.PREVIEW_2C_GITBOOK_URL }}
|
||||
GITBOOK_IMAGE_RESIZE_SIGNING_KEY: ${{ secrets.PREVIEW_GITBOOK_IMAGE_RESIZE_SIGNING_KEY }}
|
||||
GITBOOK_IMAGE_RESIZE_URL: ${{ vars.GITBOOK_IMAGE_RESIZE_URL }}
|
||||
GITBOOK_ICONS_URL: ${{ vars.GITBOOK_ICONS_URL }}
|
||||
GITBOOK_ICONS_TOKEN: ${{ vars.GITBOOK_ICONS_TOKEN }}
|
||||
GITBOOK_SECRET: ${{ github.ref == 'refs/heads/main' && secrets.PRODUCTION_GITBOOK_SECRET|| '' }}
|
||||
opItem: op://gitbook-open/2c-preview
|
||||
opServiceAccount: ${{ secrets.OP_SERVICE_ACCOUNT_TOKEN }}
|
||||
- name: Outputs
|
||||
run: |
|
||||
echo "URL: ${{ steps.deploy.outputs.deployment-url }}"
|
||||
|
||||
@@ -9,6 +9,9 @@ jobs:
|
||||
deploy-v2-vercel:
|
||||
name: Deploy v2 to Vercel (production)
|
||||
runs-on: ubuntu-latest
|
||||
environment:
|
||||
name: 2v-production
|
||||
url: ${{ steps.deploy.outputs.deployment-url }}
|
||||
outputs:
|
||||
deployment-url: ${{ steps.deploy.outputs.deployment-url }}
|
||||
steps:
|
||||
@@ -18,13 +21,18 @@ jobs:
|
||||
id: deploy
|
||||
uses: ./.github/composite/deploy-vercel
|
||||
with:
|
||||
environment: production
|
||||
vercel-org: ${{ secrets.VERCEL_ORG_ID }}
|
||||
vercel-project: ${{ secrets.VERCEL_PROJECT_ID }}
|
||||
vercel-token: ${{ secrets.VERCEL_TOKEN }}
|
||||
environment: production
|
||||
vercelOrg: ${{ secrets.VERCEL_ORG_ID }}
|
||||
vercelProject: ${{ secrets.VERCEL_PROJECT_ID }}
|
||||
vercelToken: ${{ secrets.VERCEL_TOKEN }}
|
||||
opItem: op://gitbook-open/2v-production
|
||||
opServiceAccount: ${{ secrets.OP_SERVICE_ACCOUNT_TOKEN }}
|
||||
deploy-v2-cloudflare:
|
||||
name: Deploy v2 to Cloudflare Worker (production)
|
||||
runs-on: ubuntu-latest
|
||||
environment:
|
||||
name: 2c-production
|
||||
url: ${{ steps.deploy.outputs.deployment-url }}
|
||||
outputs:
|
||||
deployment-url: ${{ steps.deploy.outputs.deployment-url }}
|
||||
steps:
|
||||
@@ -34,16 +42,12 @@ jobs:
|
||||
id: deploy
|
||||
uses: ./.github/composite/deploy-cloudflare
|
||||
with:
|
||||
environment: staging
|
||||
environment: production
|
||||
deploy: true
|
||||
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
|
||||
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
|
||||
NEXT_SERVER_ACTIONS_ENCRYPTION_KEY: ${{ secrets.NEXT_SERVER_ACTIONS_ENCRYPTION_KEY }}
|
||||
GITBOOK_URL: ${{ vars.STAGING_2C_GITBOOK_URL }}
|
||||
GITBOOK_IMAGE_RESIZE_SIGNING_KEY: ${{ secrets.STAGING_GITBOOK_IMAGE_RESIZE_SIGNING_KEY }}
|
||||
GITBOOK_IMAGE_RESIZE_URL: ${{ vars.GITBOOK_IMAGE_RESIZE_URL }}
|
||||
GITBOOK_ICONS_URL: ${{ vars.GITBOOK_ICONS_URL }}
|
||||
GITBOOK_ICONS_TOKEN: ${{ vars.GITBOOK_ICONS_TOKEN }}
|
||||
GITBOOK_ASSETS_PREFIX: ${{ vars.STAGING_2C_GITBOOK_ASSETS_PREFIX }}
|
||||
opItem: op://gitbook-open/2c-production
|
||||
opServiceAccount: ${{ secrets.OP_SERVICE_ACCOUNT_TOKEN }}
|
||||
- name: Outputs
|
||||
run: |
|
||||
echo "URL: ${{ steps.deploy.outputs.deployment-url }}"
|
||||
@@ -9,6 +9,9 @@ jobs:
|
||||
deploy-v2-vercel:
|
||||
name: Deploy v2 to Vercel (staging)
|
||||
runs-on: ubuntu-latest
|
||||
environment:
|
||||
name: 2v-staging
|
||||
url: ${{ steps.deploy.outputs.deployment-url }}
|
||||
outputs:
|
||||
deployment-url: ${{ steps.deploy.outputs.deployment-url }}
|
||||
steps:
|
||||
@@ -18,13 +21,18 @@ jobs:
|
||||
id: deploy
|
||||
uses: ./.github/composite/deploy-vercel
|
||||
with:
|
||||
environment: staging
|
||||
vercel-org: ${{ secrets.VERCEL_ORG_ID }}
|
||||
vercel-project: ${{ secrets.VERCEL_PROJECT_ID }}
|
||||
vercel-token: ${{ secrets.VERCEL_TOKEN }}
|
||||
environment: staging
|
||||
vercelOrg: ${{ secrets.VERCEL_ORG_ID }}
|
||||
vercelProject: ${{ secrets.VERCEL_PROJECT_ID }}
|
||||
vercelToken: ${{ secrets.VERCEL_TOKEN }}
|
||||
opItem: op://gitbook-open/2v-staging
|
||||
opServiceAccount: ${{ secrets.OP_SERVICE_ACCOUNT_TOKEN }}
|
||||
deploy-v2-cloudflare:
|
||||
name: Deploy v2 to Cloudflare Worker (staging)
|
||||
runs-on: ubuntu-latest
|
||||
environment:
|
||||
name: 2c-staging
|
||||
url: ${{ steps.deploy.outputs.deployment-url }}
|
||||
outputs:
|
||||
deployment-url: ${{ steps.deploy.outputs.deployment-url }}
|
||||
steps:
|
||||
@@ -38,17 +46,8 @@ jobs:
|
||||
deploy: true
|
||||
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
|
||||
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
|
||||
NEXT_SERVER_ACTIONS_ENCRYPTION_KEY: ${{ secrets.NEXT_SERVER_ACTIONS_ENCRYPTION_KEY }}
|
||||
GITBOOK_URL: ${{ vars.STAGING_2C_GITBOOK_URL }}
|
||||
GITBOOK_SECRET: ${{ secrets.STAGING_GITBOOK_SECRET }}
|
||||
GITBOOK_APP_URL: https://app.gitbook-staging.com
|
||||
GITBOOK_API_URL: https://api.gitbook-staging.com
|
||||
GITBOOK_INTEGRATIONS_HOST: https://integrations.gitbook-staging.com
|
||||
GITBOOK_IMAGE_RESIZE_SIGNING_KEY: ${{ secrets.STAGING_GITBOOK_IMAGE_RESIZE_SIGNING_KEY }}
|
||||
GITBOOK_IMAGE_RESIZE_URL: ${{ vars.GITBOOK_IMAGE_RESIZE_URL }}
|
||||
GITBOOK_ICONS_URL: ${{ vars.GITBOOK_ICONS_URL }}
|
||||
GITBOOK_ICONS_TOKEN: ${{ vars.GITBOOK_ICONS_TOKEN }}
|
||||
GITBOOK_ASSETS_PREFIX: ${{ vars.STAGING_2C_GITBOOK_ASSETS_PREFIX }}
|
||||
opItem: op://gitbook-open/2c-staging
|
||||
opServiceAccount: ${{ secrets.OP_SERVICE_ACCOUNT_TOKEN }}
|
||||
- name: Outputs
|
||||
run: |
|
||||
echo "URL: ${{ steps.deploy.outputs.deployment-url }}"
|
||||
@@ -10,16 +10,21 @@ const nextConfig = {
|
||||
|
||||
env: {
|
||||
BUILD_VERSION: (process.env.GITHUB_SHA ?? '').slice(0, 7),
|
||||
GITBOOK_URL: process.env.GITBOOK_URL,
|
||||
GITBOOK_SECRET: process.env.GITBOOK_SECRET,
|
||||
|
||||
// GitBook envs
|
||||
GITBOOK_API_URL: process.env.GITBOOK_API_URL,
|
||||
GITBOOK_APP_URL: process.env.GITBOOK_APP_URL,
|
||||
GITBOOK_INTEGRATIONS_HOST: process.env.GITBOOK_INTEGRATIONS_HOST,
|
||||
GITBOOK_IMAGE_RESIZE_SIGNING_KEY: process.env.GITBOOK_IMAGE_RESIZE_SIGNING_KEY,
|
||||
GITBOOK_IMAGE_RESIZE_URL: process.env.GITBOOK_IMAGE_RESIZE_URL,
|
||||
GITBOOK_ASSETS_PREFIX: process.env.GITBOOK_ASSETS_PREFIX,
|
||||
GITBOOK_ICONS_URL: process.env.GITBOOK_ICONS_URL,
|
||||
GITBOOK_ICONS_TOKEN: process.env.GITBOOK_ICONS_TOKEN,
|
||||
GITBOOK_URL: process.env.GITBOOK_URL,
|
||||
GITBOOK_API_TOKEN: process.env.GITBOOK_API_TOKEN,
|
||||
GITBOOK_ASSETS_PREFIX: process.env.GITBOOK_ASSETS_PREFIX,
|
||||
GITBOOK_SECRET: process.env.GITBOOK_SECRET,
|
||||
GITBOOK_IMAGE_RESIZE_SIGNING_KEY: process.env.GITBOOK_IMAGE_RESIZE_SIGNING_KEY,
|
||||
|
||||
// Next.js envs
|
||||
NEXT_SERVER_ACTIONS_ENCRYPTION_KEY: process.env.NEXT_SERVER_ACTIONS_ENCRYPTION_KEY,
|
||||
|
||||
// Used to detect if the app is running in V2 mode
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
import { type NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
import {
|
||||
GITBOOK_API_TOKEN,
|
||||
GITBOOK_API_URL,
|
||||
GITBOOK_APP_URL,
|
||||
GITBOOK_ASSETS_URL,
|
||||
GITBOOK_DISABLE_TRACKING,
|
||||
GITBOOK_ICONS_URL,
|
||||
GITBOOK_IMAGE_RESIZE_SIGNING_KEY,
|
||||
GITBOOK_INTEGRATIONS_HOST,
|
||||
GITBOOK_SECRET,
|
||||
GITBOOK_URL,
|
||||
GITBOOK_USER_AGENT,
|
||||
} from '@v2/lib/env';
|
||||
@@ -19,8 +23,14 @@ export async function GET(_req: NextRequest) {
|
||||
GITBOOK_APP_URL,
|
||||
GITBOOK_API_URL,
|
||||
GITBOOK_ASSETS_URL,
|
||||
GITBOOK_ICONS_URL,
|
||||
GITBOOK_USER_AGENT,
|
||||
GITBOOK_INTEGRATIONS_HOST,
|
||||
GITBOOK_DISABLE_TRACKING,
|
||||
|
||||
// Secret envs
|
||||
GITBOOK_SECRET: !!GITBOOK_SECRET,
|
||||
GITBOOK_API_TOKEN: !!GITBOOK_API_TOKEN,
|
||||
GITBOOK_IMAGE_RESIZE_SIGNING_KEY: !!GITBOOK_IMAGE_RESIZE_SIGNING_KEY,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -3,3 +3,4 @@ export * from './types';
|
||||
export * from './pages';
|
||||
export * from './urls';
|
||||
export * from './errors';
|
||||
export * from './lookup';
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
import { race, tryCatch } from '@/lib/async';
|
||||
import { joinPath } from '@/lib/paths';
|
||||
import { GitBookAPI, type GitBookAPIError, type PublishedSiteContentLookup } from '@gitbook/api';
|
||||
import { GITBOOK_API_TOKEN, GITBOOK_API_URL, GITBOOK_USER_AGENT } from '@v2/lib/env';
|
||||
import { getURLLookupAlternatives, stripURLSearch } from './urls';
|
||||
|
||||
/**
|
||||
* Lookup a content by its URL using the GitBook API.
|
||||
* To optimize caching, we try multiple lookup alternatives and return the first one that matches.
|
||||
*/
|
||||
export async function getPublishedContentByURL(input: {
|
||||
url: string;
|
||||
visitorAuthToken: string | null;
|
||||
redirectOnError: boolean;
|
||||
}): Promise<
|
||||
| { data: PublishedSiteContentLookup; error?: undefined }
|
||||
| { data?: undefined; error: Error | GitBookAPIError }
|
||||
> {
|
||||
const lookupURL = new URL(input.url);
|
||||
const url = stripURLSearch(lookupURL);
|
||||
const lookup = getURLLookupAlternatives(url);
|
||||
|
||||
const result = await race(lookup.urls, async (alternative, { signal }) => {
|
||||
const api = new GitBookAPI({
|
||||
authToken: GITBOOK_API_TOKEN ?? undefined,
|
||||
endpoint: GITBOOK_API_URL,
|
||||
userAgent: GITBOOK_USER_AGENT,
|
||||
});
|
||||
|
||||
const callResult = await tryCatch(
|
||||
api.urls.getPublishedContentByUrl(
|
||||
{
|
||||
url: alternative.url,
|
||||
visitorAuthToken: input.visitorAuthToken ?? undefined,
|
||||
redirectOnError: input.redirectOnError,
|
||||
cache: true,
|
||||
},
|
||||
{
|
||||
signal,
|
||||
headers: {
|
||||
'x-gitbook-force-cache': 'true',
|
||||
},
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
if (callResult.error) {
|
||||
if (alternative.primary) {
|
||||
// We only return an error for the primary alternative (full URL),
|
||||
// as other parts could result in errors due to the URL being incomplete (share links, etc).
|
||||
return { error: callResult.error };
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
const {
|
||||
data: { data },
|
||||
} = callResult;
|
||||
|
||||
if ('redirect' in data) {
|
||||
if (alternative.primary) {
|
||||
// Append the path to the redirect URL
|
||||
// because we might have matched a shorter path and the redirect is relative to it
|
||||
if (alternative.extraPath) {
|
||||
if (data.target === 'content') {
|
||||
const redirect = new URL(data.redirect);
|
||||
redirect.pathname = joinPath(redirect.pathname, alternative.extraPath);
|
||||
data.redirect = redirect.toString();
|
||||
} else {
|
||||
const redirect = new URL(data.redirect);
|
||||
if (redirect.searchParams.has('location')) {
|
||||
redirect.searchParams.set(
|
||||
'location',
|
||||
joinPath(
|
||||
redirect.searchParams.get('location') ?? '',
|
||||
alternative.extraPath
|
||||
)
|
||||
);
|
||||
data.redirect = redirect.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { data };
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* We use the following criteria to determine if the lookup result is the right one:
|
||||
* - the primary alternative was resolved (because that's the longest or most inclusive path)
|
||||
* - the resolution of the site URL is complete (because we want to resolve the deepest path possible)
|
||||
*
|
||||
* In both cases, the idea is to use the deepest/longest/most inclusive path to resolve the content.
|
||||
*/
|
||||
if (alternative.primary || ('site' in data && data.complete)) {
|
||||
const siteResult: PublishedSiteContentLookup = {
|
||||
...data,
|
||||
changeRequest: data.changeRequest ?? lookup.changeRequest,
|
||||
revision: data.revision ?? lookup.revision,
|
||||
basePath: joinPath(data.basePath, lookup.basePath ?? ''),
|
||||
pathname: joinPath(data.pathname, alternative.extraPath),
|
||||
};
|
||||
return { data: siteResult };
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
|
||||
if (!result) {
|
||||
return {
|
||||
error: new Error('No content found'),
|
||||
};
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -1,121 +1,3 @@
|
||||
import { race, tryCatch } from '@/lib/async';
|
||||
import { joinPath } from '@/lib/paths';
|
||||
import { GitBookAPI, type GitBookAPIError, type PublishedSiteContentLookup } from '@gitbook/api';
|
||||
import { GITBOOK_API_TOKEN, GITBOOK_API_URL, GITBOOK_USER_AGENT } from '@v2/lib/env';
|
||||
|
||||
/**
|
||||
* Lookup a content by its URL using the GitBook API.
|
||||
* To optimize caching, we try multiple lookup alternatives and return the first one that matches.
|
||||
*/
|
||||
export async function getPublishedContentByURL(input: {
|
||||
url: string;
|
||||
visitorAuthToken: string | null;
|
||||
redirectOnError: boolean;
|
||||
}): Promise<
|
||||
| { data: PublishedSiteContentLookup; error?: undefined }
|
||||
| { data?: undefined; error: Error | GitBookAPIError }
|
||||
> {
|
||||
const lookupURL = new URL(input.url);
|
||||
const url = stripURLSearch(lookupURL);
|
||||
const lookup = getURLLookupAlternatives(url);
|
||||
|
||||
const result = await race(lookup.urls, async (alternative, { signal }) => {
|
||||
const api = new GitBookAPI({
|
||||
authToken: GITBOOK_API_TOKEN ?? undefined,
|
||||
endpoint: GITBOOK_API_URL,
|
||||
userAgent: GITBOOK_USER_AGENT,
|
||||
});
|
||||
|
||||
const callResult = await tryCatch(
|
||||
api.urls.getPublishedContentByUrl(
|
||||
{
|
||||
url: alternative.url,
|
||||
visitorAuthToken: input.visitorAuthToken ?? undefined,
|
||||
redirectOnError: input.redirectOnError,
|
||||
cache: true,
|
||||
},
|
||||
{
|
||||
signal,
|
||||
headers: {
|
||||
'x-gitbook-force-cache': 'true',
|
||||
},
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
if (callResult.error) {
|
||||
if (alternative.primary) {
|
||||
// We only return an error for the primary alternative (full URL),
|
||||
// as other parts could result in errors due to the URL being incomplete (share links, etc).
|
||||
return { error: callResult.error };
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
const {
|
||||
data: { data },
|
||||
} = callResult;
|
||||
|
||||
if ('redirect' in data) {
|
||||
if (alternative.primary) {
|
||||
// Append the path to the redirect URL
|
||||
// because we might have matched a shorter path and the redirect is relative to it
|
||||
if (alternative.extraPath) {
|
||||
if (data.target === 'content') {
|
||||
const redirect = new URL(data.redirect);
|
||||
redirect.pathname = joinPath(redirect.pathname, alternative.extraPath);
|
||||
data.redirect = redirect.toString();
|
||||
} else {
|
||||
const redirect = new URL(data.redirect);
|
||||
if (redirect.searchParams.has('location')) {
|
||||
redirect.searchParams.set(
|
||||
'location',
|
||||
joinPath(
|
||||
redirect.searchParams.get('location') ?? '',
|
||||
alternative.extraPath
|
||||
)
|
||||
);
|
||||
data.redirect = redirect.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { data };
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* We use the following criteria to determine if the lookup result is the right one:
|
||||
* - the primary alternative was resolved (because that's the longest or most inclusive path)
|
||||
* - the resolution of the site URL is complete (because we want to resolve the deepest path possible)
|
||||
*
|
||||
* In both cases, the idea is to use the deepest/longest/most inclusive path to resolve the content.
|
||||
*/
|
||||
if (alternative.primary || ('site' in data && data.complete)) {
|
||||
const siteResult: PublishedSiteContentLookup = {
|
||||
...data,
|
||||
changeRequest: data.changeRequest ?? lookup.changeRequest,
|
||||
revision: data.revision ?? lookup.revision,
|
||||
basePath: joinPath(data.basePath, lookup.basePath ?? ''),
|
||||
pathname: joinPath(data.pathname, alternative.extraPath),
|
||||
};
|
||||
return { data: siteResult };
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
|
||||
if (!result) {
|
||||
return {
|
||||
error: new Error('No content found'),
|
||||
};
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* For a given GitBook URL, return a list of alternative URLs that could be matched against to lookup the content.
|
||||
* The approach is optimized to aim at reusing cached lookup results as much as possible.
|
||||
|
||||
+22
-3
@@ -1,3 +1,11 @@
|
||||
import 'server-only';
|
||||
|
||||
/*
|
||||
* Support both Cloudflare and Vercel, environment variables can be bundled.
|
||||
* To avoid leaking them on the client-side, they should be accessed from this file
|
||||
* and not from the `process.env` object.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Main host on which GitBook is running.
|
||||
*/
|
||||
@@ -14,12 +22,12 @@ export const GITBOOK_URL =
|
||||
export const GITBOOK_ASSETS_URL =
|
||||
process.env.NODE_ENV === 'development'
|
||||
? 'http://localhost:3000'
|
||||
: process.env.GITBOOK_ASSETS_PREFIX || GITBOOK_URL;
|
||||
: process.env.GITBOOK_ASSETS_PREFIX;
|
||||
|
||||
/**
|
||||
* GitBook app URL.
|
||||
*/
|
||||
export const GITBOOK_APP_URL = process.env.NEXT_PUBLIC_GITBOOK_APP_URL || 'https://app.gitbook.com';
|
||||
export const GITBOOK_APP_URL = process.env.GITBOOK_APP_URL || 'https://app.gitbook.com';
|
||||
|
||||
/**
|
||||
* Default GitBook API URL endpoint.
|
||||
@@ -30,7 +38,7 @@ export const GITBOOK_API_URL = process.env.GITBOOK_API_URL || 'https://api.gitbo
|
||||
* Default GitBook API token.
|
||||
* It can be use to avoid rate-limiting.
|
||||
*/
|
||||
export const GITBOOK_API_TOKEN = process.env.GITBOOK_API_TOKEN ?? null;
|
||||
export const GITBOOK_API_TOKEN = process.env.GITBOOK_API_TOKEN || null;
|
||||
|
||||
/**
|
||||
* User agent to use for API requests.
|
||||
@@ -59,6 +67,17 @@ export const GITBOOK_IMAGE_RESIZE_URL = process.env.GITBOOK_IMAGE_RESIZE_URL ??
|
||||
export const GITBOOK_IMAGE_RESIZE_SIGNING_KEY =
|
||||
process.env.GITBOOK_IMAGE_RESIZE_SIGNING_KEY ?? null;
|
||||
|
||||
/**
|
||||
* Endpoint where icons are served.
|
||||
*/
|
||||
export const GITBOOK_ICONS_URL =
|
||||
process.env.GITBOOK_ICONS_URL || `${GITBOOK_ASSETS_URL || ''}/~gitbook/static/icons`;
|
||||
|
||||
/**
|
||||
* Token passed to the icons endpoint.
|
||||
*/
|
||||
export const GITBOOK_ICONS_TOKEN = process.env.GITBOOK_ICONS_TOKEN;
|
||||
|
||||
/**
|
||||
* Secret used to validate requests from the GitBook app.
|
||||
*/
|
||||
|
||||
@@ -1,5 +1,20 @@
|
||||
# gitbook
|
||||
|
||||
## 0.7.2
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- f127d28: Rename OpenAPIModels to OpenAPISchemas
|
||||
- Updated dependencies [f127d28]
|
||||
- @gitbook/react-openapi@1.1.2
|
||||
|
||||
## 0.7.1
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [f574858]
|
||||
- @gitbook/react-openapi@1.1.1
|
||||
|
||||
## 0.7.0
|
||||
|
||||
### Minor Changes
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "gitbook",
|
||||
"version": "0.7.0",
|
||||
"version": "0.7.2",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"dev": "env-cmd --silent -f ../../.env.local next dev",
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
@apply flex-1 flex flex-col gap-4 mb-14;
|
||||
}
|
||||
|
||||
.openapi-models {
|
||||
.openapi-schemas {
|
||||
@apply flex flex-col mb-14 flex-1;
|
||||
}
|
||||
|
||||
@@ -620,10 +620,10 @@
|
||||
@apply space-y-2.5;
|
||||
}
|
||||
|
||||
.openapi-section-models {
|
||||
.openapi-section-schemas {
|
||||
@apply border border-tint-subtle rounded-lg;
|
||||
}
|
||||
|
||||
.openapi-section-models > .openapi-section-body > .openapi-schema-properties > .openapi-schema {
|
||||
.openapi-section-schemas > .openapi-section-body > .openapi-schema-properties > .openapi-schema {
|
||||
@apply p-2.5;
|
||||
}
|
||||
|
||||
@@ -63,6 +63,7 @@ const InsightsContext = React.createContext<TrackEventCallback>(() => {});
|
||||
|
||||
interface InsightsProviderProps extends InsightsEventContext {
|
||||
enabled: boolean;
|
||||
appURL: string;
|
||||
apiHost: string;
|
||||
visitorAuthToken: string | null;
|
||||
children: React.ReactNode;
|
||||
@@ -72,7 +73,7 @@ interface InsightsProviderProps extends InsightsEventContext {
|
||||
* Wrap the content of the app with the InsightsProvider to track events.
|
||||
*/
|
||||
export function InsightsProvider(props: InsightsProviderProps) {
|
||||
const { enabled, apiHost, visitorAuthToken, children, ...context } = props;
|
||||
const { enabled, appURL, apiHost, visitorAuthToken, children, ...context } = props;
|
||||
|
||||
const visitorIdRef = React.useRef<string | null>(null);
|
||||
const eventsRef = React.useRef<{
|
||||
@@ -140,7 +141,7 @@ export function InsightsProvider(props: InsightsProviderProps) {
|
||||
});
|
||||
|
||||
const flushBatchedEvents = useDebounceCallback(async () => {
|
||||
const visitorId = visitorIdRef.current ?? (await getVisitorId());
|
||||
const visitorId = visitorIdRef.current ?? (await getVisitorId(appURL));
|
||||
visitorIdRef.current = visitorId;
|
||||
|
||||
flushEventsSync();
|
||||
@@ -184,7 +185,7 @@ export function InsightsProvider(props: InsightsProviderProps) {
|
||||
* Get the visitor ID and store it in a ref.
|
||||
*/
|
||||
React.useEffect(() => {
|
||||
getVisitorId().then((visitorId) => {
|
||||
getVisitorId(appURL).then((visitorId) => {
|
||||
visitorIdRef.current = visitorId;
|
||||
// When the page is unloaded, flush all events, but only if the visitor ID is set
|
||||
window.addEventListener('beforeunload', flushEventsSync);
|
||||
@@ -192,7 +193,7 @@ export function InsightsProvider(props: InsightsProviderProps) {
|
||||
return () => {
|
||||
window.removeEventListener('beforeunload', flushEventsSync);
|
||||
};
|
||||
}, [flushEventsSync]);
|
||||
}, [flushEventsSync, appURL]);
|
||||
|
||||
return (
|
||||
<InsightsContext.Provider value={trackEvent}>
|
||||
|
||||
@@ -13,10 +13,10 @@ let pendingVisitorId: Promise<string> | null = null;
|
||||
/**
|
||||
* Return the current visitor identifier.
|
||||
*/
|
||||
export async function getVisitorId(): Promise<string> {
|
||||
export async function getVisitorId(appURL: string): Promise<string> {
|
||||
if (!visitorId) {
|
||||
if (!pendingVisitorId) {
|
||||
pendingVisitorId = fetchVisitorID().finally(() => {
|
||||
pendingVisitorId = fetchVisitorID(appURL).finally(() => {
|
||||
pendingVisitorId = null;
|
||||
});
|
||||
}
|
||||
@@ -30,7 +30,7 @@ export async function getVisitorId(): Promise<string> {
|
||||
/**
|
||||
* Propose a visitor identifier to the GitBook.com server and get the devideId back.
|
||||
*/
|
||||
async function fetchVisitorID(): Promise<string> {
|
||||
async function fetchVisitorID(appURL: string): Promise<string> {
|
||||
const withoutCookies = isCookiesTrackingDisabled();
|
||||
|
||||
if (withoutCookies) {
|
||||
@@ -46,7 +46,7 @@ async function fetchVisitorID(): Promise<string> {
|
||||
// No tracking deviceId set, we'll need to consolidate with the server.
|
||||
const proposed = generateRandomId();
|
||||
|
||||
const url = new URL(process.env.NEXT_PUBLIC_GITBOOK_APP_URL ?? 'https://app.gitbook.com');
|
||||
const url = new URL(appURL);
|
||||
url.pathname = '/__session';
|
||||
url.searchParams.set('proposed', proposed);
|
||||
|
||||
|
||||
@@ -31,6 +31,7 @@ import { ClientContexts } from './ClientContexts';
|
||||
|
||||
import '@gitbook/icons/style.css';
|
||||
import './globals.css';
|
||||
import { GITBOOK_ICONS_TOKEN, GITBOOK_ICONS_URL } from '@v2/lib/env';
|
||||
|
||||
/**
|
||||
* Layout shared between the content and the PDF renderer.
|
||||
@@ -127,8 +128,8 @@ export async function CustomizationRootLayout(props: {
|
||||
)}
|
||||
>
|
||||
<IconsProvider
|
||||
assetsURL={process.env.GITBOOK_ICONS_URL ?? getAssetURL('icons')}
|
||||
assetsURLToken={process.env.GITBOOK_ICONS_TOKEN}
|
||||
assetsURL={GITBOOK_ICONS_URL}
|
||||
assetsURLToken={GITBOOK_ICONS_TOKEN}
|
||||
assetsByStyles={{
|
||||
'custom-icons': {
|
||||
assetsURL: getAssetURL('icons'),
|
||||
|
||||
@@ -11,6 +11,7 @@ import { getSpaceLanguage } from '@/intl/server';
|
||||
import { t } from '@/intl/translate';
|
||||
import { tcls } from '@/lib/tailwind';
|
||||
|
||||
import { GITBOOK_APP_URL } from '@v2/lib/env';
|
||||
import { SpacesDropdown } from '../Header/SpacesDropdown';
|
||||
import { InsightsProvider } from '../Insights';
|
||||
import { SiteSectionList, encodeClientSiteSections } from '../SiteSections';
|
||||
@@ -47,6 +48,7 @@ export function SpaceLayout(props: {
|
||||
return (
|
||||
<InsightsProvider
|
||||
enabled={withTracking}
|
||||
appURL={GITBOOK_APP_URL}
|
||||
apiHost={context.dataFetcher.apiEndpoint}
|
||||
visitorAuthToken={visitorAuthToken}
|
||||
organizationId={context.organizationId}
|
||||
|
||||
@@ -4,5 +4,5 @@ import { GITBOOK_ASSETS_URL } from '@v2/lib/env';
|
||||
* Create a public URL for an asset.
|
||||
*/
|
||||
export function getAssetURL(path: string): string {
|
||||
return `${GITBOOK_ASSETS_URL}/~gitbook/static/${path}`;
|
||||
return `${GITBOOK_ASSETS_URL || ''}/~gitbook/static/${path}`;
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
} from '@gitbook/api';
|
||||
import { headers } from 'next/headers';
|
||||
|
||||
import { GITBOOK_APP_URL } from '@v2/lib/env';
|
||||
import { getPagePath } from './pages';
|
||||
import { assertIsNotV2 } from './v2';
|
||||
|
||||
@@ -100,7 +101,7 @@ export async function getAbsoluteHref(href: string, withHost = false): Promise<s
|
||||
* Create an absolute href in the GitBook application.
|
||||
*/
|
||||
export function getGitbookAppHref(pathname: string): string {
|
||||
const appUrl = new URL(process.env.NEXT_PUBLIC_GITBOOK_APP_URL ?? 'https://app.gitbook.com');
|
||||
const appUrl = new URL(GITBOOK_APP_URL);
|
||||
appUrl.pathname = pathname;
|
||||
|
||||
return appUrl.toString();
|
||||
|
||||
+10
-10
@@ -1,35 +1,35 @@
|
||||
import { fetchOpenAPIFilesystem } from '@/lib/openapi/fetch';
|
||||
import type { ResolveOpenAPIBlockResult } from '@/lib/openapi/types';
|
||||
import { OpenAPIParseError } from '@gitbook/openapi-parser';
|
||||
import { type OpenAPIModelsData, resolveOpenAPIModels } from '@gitbook/react-openapi';
|
||||
import { type OpenAPISchemasData, resolveOpenAPISchemas } from '@gitbook/react-openapi';
|
||||
import type { AnyOpenAPIBlock, ResolveOpenAPIBlockArgs } from './types';
|
||||
|
||||
type ResolveOpenAPIModelsBlockResult = ResolveOpenAPIBlockResult<OpenAPIModelsData>;
|
||||
type ResolveOpenAPISchemasBlockResult = ResolveOpenAPIBlockResult<OpenAPISchemasData>;
|
||||
|
||||
const weakmap = new WeakMap<AnyOpenAPIBlock, Promise<ResolveOpenAPIModelsBlockResult>>();
|
||||
const weakmap = new WeakMap<AnyOpenAPIBlock, Promise<ResolveOpenAPISchemasBlockResult>>();
|
||||
|
||||
/**
|
||||
* Cache the result of resolving an OpenAPI block.
|
||||
* It is important because the resolve is called in sections and in the block itself.
|
||||
*/
|
||||
export function resolveOpenAPIModelsBlock(
|
||||
export function resolveOpenAPISchemasBlock(
|
||||
args: ResolveOpenAPIBlockArgs
|
||||
): Promise<ResolveOpenAPIModelsBlockResult> {
|
||||
): Promise<ResolveOpenAPISchemasBlockResult> {
|
||||
if (weakmap.has(args.block)) {
|
||||
return weakmap.get(args.block)!;
|
||||
}
|
||||
|
||||
const result = baseResolveOpenAPIModelsBlock(args);
|
||||
const result = baseResolveOpenAPISchemasBlock(args);
|
||||
weakmap.set(args.block, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve OpenAPI models block.
|
||||
* Resolve OpenAPI schemas block.
|
||||
*/
|
||||
async function baseResolveOpenAPIModelsBlock(
|
||||
async function baseResolveOpenAPISchemasBlock(
|
||||
args: ResolveOpenAPIBlockArgs
|
||||
): Promise<ResolveOpenAPIModelsBlockResult> {
|
||||
): Promise<ResolveOpenAPISchemasBlockResult> {
|
||||
const { context, block } = args;
|
||||
if (!block.data.path || !block.data.method) {
|
||||
return { data: null, specUrl: null };
|
||||
@@ -42,7 +42,7 @@ async function baseResolveOpenAPIModelsBlock(
|
||||
return { data: null, specUrl: null };
|
||||
}
|
||||
|
||||
const data = await resolveOpenAPIModels(filesystem);
|
||||
const data = await resolveOpenAPISchemas(filesystem);
|
||||
|
||||
return { data, specUrl };
|
||||
} catch (error) {
|
||||
@@ -2,7 +2,7 @@ import type { DocumentBlockOpenAPI, DocumentBlockOpenAPIOperation } from '@gitbo
|
||||
import type { Filesystem, OpenAPIParseError, OpenAPIV3xDocument } from '@gitbook/openapi-parser';
|
||||
import type { GitBookAnyContext } from '@v2/lib/context';
|
||||
|
||||
//!!TODO: Add DocumentBlockOpenAPIModels when available in @gitbook/api
|
||||
//!!TODO: Add DocumentBlockOpenAPISchemas when available in @gitbook/api
|
||||
export type AnyOpenAPIBlock = DocumentBlockOpenAPI | DocumentBlockOpenAPIOperation;
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,17 @@
|
||||
# @gitbook/react-openapi
|
||||
|
||||
## 1.1.2
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- f127d28: Rename OpenAPIModels to OpenAPISchemas
|
||||
|
||||
## 1.1.1
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- f574858: Fix OpenAPI example display error
|
||||
|
||||
## 1.1.0
|
||||
|
||||
### Minor Changes
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
"default": "./dist/index.js"
|
||||
}
|
||||
},
|
||||
"version": "1.1.0",
|
||||
"version": "1.1.2",
|
||||
"sideEffects": false,
|
||||
"dependencies": {
|
||||
"@gitbook/openapi-parser": "workspace:*",
|
||||
|
||||
@@ -217,7 +217,7 @@ function OpenAPISchemaPresentation(props: { property: OpenAPISchemaPropertyEntry
|
||||
propertyName={propertyName}
|
||||
required={required}
|
||||
/>
|
||||
{schema['x-deprecated-sunset'] ? (
|
||||
{typeof schema['x-deprecated-sunset'] === 'string' ? (
|
||||
<div className="openapi-deprecated-sunset openapi-schema-description openapi-markdown">
|
||||
Sunset date:{' '}
|
||||
<span className="openapi-deprecated-sunset-date">
|
||||
@@ -228,7 +228,7 @@ function OpenAPISchemaPresentation(props: { property: OpenAPISchemaPropertyEntry
|
||||
{description ? (
|
||||
<Markdown source={description} className="openapi-schema-description" />
|
||||
) : null}
|
||||
{example ? (
|
||||
{typeof example === 'string' ? (
|
||||
<div className="openapi-schema-example">
|
||||
Example: <code>{example}</code>
|
||||
</div>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
export * from './models';
|
||||
export * from './schemas';
|
||||
export * from './OpenAPIOperation';
|
||||
export * from './OpenAPIOperationContext';
|
||||
export * from './resolveOpenAPIOperation';
|
||||
export type { OpenAPIModelsData, OpenAPIOperationData } from './types';
|
||||
export type { OpenAPISchemasData, OpenAPIOperationData } from './types';
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
export * from './OpenAPIModels';
|
||||
export * from './resolveOpenAPIModels';
|
||||
+18
-18
@@ -2,18 +2,18 @@ import clsx from 'clsx';
|
||||
import { OpenAPIDisclosureGroup } from '../OpenAPIDisclosureGroup';
|
||||
import { OpenAPIRootSchema } from '../OpenAPISchema';
|
||||
import { Section, SectionBody } from '../StaticSection';
|
||||
import type { OpenAPIClientContext, OpenAPIContextProps, OpenAPIModelsData } from '../types';
|
||||
import type { OpenAPIClientContext, OpenAPIContextProps, OpenAPISchemasData } from '../types';
|
||||
|
||||
/**
|
||||
* Display OpenAPI Models.
|
||||
* Display OpenAPI Schemas.
|
||||
*/
|
||||
export function OpenAPIModels(props: {
|
||||
export function OpenAPISchemas(props: {
|
||||
className?: string;
|
||||
data: OpenAPIModelsData;
|
||||
data: OpenAPISchemasData;
|
||||
context: OpenAPIContextProps;
|
||||
}) {
|
||||
const { className, data, context } = props;
|
||||
const { models } = data;
|
||||
const { schemas } = data;
|
||||
|
||||
const clientContext: OpenAPIClientContext = {
|
||||
defaultInteractiveOpened: context.defaultInteractiveOpened,
|
||||
@@ -21,30 +21,30 @@ export function OpenAPIModels(props: {
|
||||
blockKey: context.blockKey,
|
||||
};
|
||||
|
||||
if (!models.length) {
|
||||
if (!schemas.length) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={clsx('openapi-models', className)}>
|
||||
<OpenAPIRootModelsSchema models={models} context={clientContext} />
|
||||
<div className={clsx('openapi-schemas', className)}>
|
||||
<OpenAPIRootSchemasSchema schemas={schemas} context={clientContext} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Root schema for OpenAPI models.
|
||||
* It displays a single model or a disclosure group for multiple models.
|
||||
* Root schema for OpenAPI schemas.
|
||||
* It displays a single model or a disclosure group for multiple schemas.
|
||||
*/
|
||||
function OpenAPIRootModelsSchema(props: {
|
||||
models: OpenAPIModelsData['models'];
|
||||
function OpenAPIRootSchemasSchema(props: {
|
||||
schemas: OpenAPISchemasData['schemas'];
|
||||
context: OpenAPIClientContext;
|
||||
}) {
|
||||
const { models, context } = props;
|
||||
const { schemas, context } = props;
|
||||
|
||||
// If there is only one model, we show it directly.
|
||||
if (models.length === 1) {
|
||||
const schema = models?.[0]?.schema;
|
||||
if (schemas.length === 1) {
|
||||
const schema = schemas?.[0]?.schema;
|
||||
|
||||
if (!schema) {
|
||||
return null;
|
||||
@@ -59,12 +59,12 @@ function OpenAPIRootModelsSchema(props: {
|
||||
);
|
||||
}
|
||||
|
||||
// If there are multiple models, we use a disclosure group to show them all.
|
||||
// If there are multiple schemas, we use a disclosure group to show them all.
|
||||
return (
|
||||
<OpenAPIDisclosureGroup
|
||||
allowsMultipleExpanded
|
||||
icon={context.icons.chevronRight}
|
||||
groups={models.map(({ name, schema }) => ({
|
||||
groups={schemas.map(({ name, schema }) => ({
|
||||
id: name,
|
||||
label: (
|
||||
<div className="openapi-response-tab-content" key={`model-${name}`}>
|
||||
@@ -75,7 +75,7 @@ function OpenAPIRootModelsSchema(props: {
|
||||
{
|
||||
id: 'model',
|
||||
body: (
|
||||
<Section className="openapi-section-models">
|
||||
<Section className="openapi-section-schemas">
|
||||
<SectionBody>
|
||||
<OpenAPIRootSchema schema={schema} context={context} />
|
||||
</SectionBody>
|
||||
@@ -0,0 +1,2 @@
|
||||
export * from './OpenAPISchemas';
|
||||
export * from './resolveOpenAPISchemas';
|
||||
+9
-9
@@ -6,28 +6,28 @@ import {
|
||||
shouldIgnoreEntity,
|
||||
} from '@gitbook/openapi-parser';
|
||||
import { dereferenceFilesystem } from '../dereference';
|
||||
import type { OpenAPIModel, OpenAPIModelsData } from '../types';
|
||||
import type { OpenAPISchema, OpenAPISchemasData } from '../types';
|
||||
|
||||
//!!TODO: We should return only the models that are used in the block. Still a WIP awaiting future work.
|
||||
//!!TODO: We should return only the schemas that are used in the block. Still a WIP awaiting future work.
|
||||
|
||||
/**
|
||||
* Resolve an OpenAPI models from a file and compile it to a more usable format.
|
||||
* Models are extracted from the OpenAPI components.schemas
|
||||
* Resolve an OpenAPI schemas from a file and compile it to a more usable format.
|
||||
* Schemas are extracted from the OpenAPI components.schemas
|
||||
*/
|
||||
export async function resolveOpenAPIModels(
|
||||
export async function resolveOpenAPISchemas(
|
||||
filesystem: Filesystem<OpenAPIV3xDocument>
|
||||
): Promise<OpenAPIModelsData | null> {
|
||||
): Promise<OpenAPISchemasData | null> {
|
||||
const schema = await dereferenceFilesystem(filesystem);
|
||||
|
||||
const models = getOpenAPIComponents(schema);
|
||||
const schemas = getOpenAPIComponents(schema);
|
||||
|
||||
return { models };
|
||||
return { schemas };
|
||||
}
|
||||
|
||||
/**
|
||||
* Get OpenAPI components.schemas that are not ignored.
|
||||
*/
|
||||
function getOpenAPIComponents(schema: OpenAPIV3.Document | OpenAPIV3_1.Document): OpenAPIModel[] {
|
||||
function getOpenAPIComponents(schema: OpenAPIV3.Document | OpenAPIV3_1.Document): OpenAPISchema[] {
|
||||
const schemas = schema.components?.schemas ?? {};
|
||||
return Object.entries(schemas)
|
||||
.filter(([, schema]) => !shouldIgnoreEntity(schema))
|
||||
@@ -56,12 +56,12 @@ export interface OpenAPIOperationData extends OpenAPICustomSpecProperties {
|
||||
securities: [string, OpenAPIV3.SecuritySchemeObject][];
|
||||
}
|
||||
|
||||
export type OpenAPIModel = {
|
||||
export type OpenAPISchema = {
|
||||
name: string;
|
||||
schema: OpenAPIV3.SchemaObject;
|
||||
};
|
||||
|
||||
export interface OpenAPIModelsData {
|
||||
/** Components schemas to be used for models */
|
||||
models: OpenAPIModel[];
|
||||
export interface OpenAPISchemasData {
|
||||
/** Components schemas to be used for schemas */
|
||||
schemas: OpenAPISchema[];
|
||||
}
|
||||
|
||||
@@ -51,12 +51,12 @@ export function extractDescriptions(object: AnyObject) {
|
||||
/**
|
||||
* Resolve the first example from an object.
|
||||
*/
|
||||
export function resolveFirstExample(object: AnyObject) {
|
||||
export function resolveFirstExample(object: AnyObject): string | undefined {
|
||||
if ('examples' in object && typeof object.examples === 'object' && object.examples) {
|
||||
const keys = Object.keys(object.examples);
|
||||
const firstKey = keys[0];
|
||||
if (firstKey && object.examples[firstKey]) {
|
||||
return object.examples[firstKey];
|
||||
return formatExample(object.examples[firstKey]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+10
-3
@@ -1,7 +1,15 @@
|
||||
{
|
||||
"$schema": "https://turbo.build/schema.json",
|
||||
"ui": "stream",
|
||||
"globalEnv": ["NODE_ENV", "CI", "ARGOS_*", "GITHUB_*", "GITBOOK_*", "SENTRY_*"],
|
||||
"globalEnv": [
|
||||
"NODE_ENV",
|
||||
"CI",
|
||||
"ARGOS_*",
|
||||
"GITHUB_*",
|
||||
"GITBOOK_*",
|
||||
"SENTRY_*",
|
||||
"NEXT_SERVER_ACTIONS_ENCRYPTION_KEY"
|
||||
],
|
||||
"tasks": {
|
||||
// Prepare the package for all other tasks
|
||||
"generate": {
|
||||
@@ -25,8 +33,7 @@
|
||||
},
|
||||
// Build the package for Cloudflare Pages
|
||||
"build:cloudflare": {
|
||||
"dependsOn": ["^build", "generate"],
|
||||
"env": ["NEXT_SERVER_ACTIONS_ENCRYPTION_KEY"]
|
||||
"dependsOn": ["^build", "generate"]
|
||||
},
|
||||
// Check the package for type errors
|
||||
"typecheck": {
|
||||
|
||||
Reference in New Issue
Block a user