|  | 
|  | 1 | +import type { ComputedContentSource } from '@gitbook/api'; | 
|  | 2 | +import assertNever from 'assert-never'; | 
|  | 3 | + | 
|  | 4 | +/** | 
|  | 5 | + * Get a stringified cache tag for a given object. | 
|  | 6 | + */ | 
|  | 7 | +export function getCacheTag( | 
|  | 8 | +    spec: /** | 
|  | 9 | +     * All data related to a user | 
|  | 10 | +     * @deprecated - in v2, no tag as this is an immutable data | 
|  | 11 | +     */ | 
|  | 12 | +        | { | 
|  | 13 | +              tag: 'user'; | 
|  | 14 | +              user: string; | 
|  | 15 | +          } | 
|  | 16 | +        /** | 
|  | 17 | +         * All data related to a space | 
|  | 18 | +         */ | 
|  | 19 | +        | { | 
|  | 20 | +              tag: 'space'; | 
|  | 21 | +              space: string; | 
|  | 22 | +          } | 
|  | 23 | +        /** | 
|  | 24 | +         * All data related to an integration. | 
|  | 25 | +         */ | 
|  | 26 | +        | { | 
|  | 27 | +              tag: 'integration'; | 
|  | 28 | +              integration: string; | 
|  | 29 | +          } | 
|  | 30 | +        /** | 
|  | 31 | +         * All data related to a change request | 
|  | 32 | +         */ | 
|  | 33 | +        | { | 
|  | 34 | +              tag: 'change-request'; | 
|  | 35 | +              space: string; | 
|  | 36 | +              changeRequest: string; | 
|  | 37 | +          } | 
|  | 38 | +        /** | 
|  | 39 | +         * Immutable data related to a revision | 
|  | 40 | +         * @deprecated - in v2, no tag as this is an immutable data | 
|  | 41 | +         */ | 
|  | 42 | +        | { | 
|  | 43 | +              tag: 'revision'; | 
|  | 44 | +              space: string; | 
|  | 45 | +              revision: string; | 
|  | 46 | +          } | 
|  | 47 | +        /** | 
|  | 48 | +         * Immutable data related to a document | 
|  | 49 | +         * @deprecated - in v2, no tag as this is an immutable data | 
|  | 50 | +         */ | 
|  | 51 | +        | { | 
|  | 52 | +              tag: 'document'; | 
|  | 53 | +              space: string; | 
|  | 54 | +              document: string; | 
|  | 55 | +          } | 
|  | 56 | +        /** | 
|  | 57 | +         * Immutable data related to a computed document | 
|  | 58 | +         * @deprecated - in v2, no tag as this is an immutable data | 
|  | 59 | +         */ | 
|  | 60 | +        | { | 
|  | 61 | +              tag: 'computed-document'; | 
|  | 62 | +              space: string; | 
|  | 63 | +              integration: string; | 
|  | 64 | +          } | 
|  | 65 | +        /** | 
|  | 66 | +         * All data related to the URL of a content | 
|  | 67 | +         */ | 
|  | 68 | +        | { | 
|  | 69 | +              tag: 'url'; | 
|  | 70 | +              hostname: string; | 
|  | 71 | +          } | 
|  | 72 | +        /** | 
|  | 73 | +         * All data related to a site | 
|  | 74 | +         */ | 
|  | 75 | +        | { | 
|  | 76 | +              tag: 'site'; | 
|  | 77 | +              site: string; | 
|  | 78 | +          } | 
|  | 79 | +        /** | 
|  | 80 | +         * All data related to an OpenAPI spec | 
|  | 81 | +         */ | 
|  | 82 | +        | { | 
|  | 83 | +              tag: 'openapi'; | 
|  | 84 | +              organization: string; | 
|  | 85 | +              openAPISpec: string; | 
|  | 86 | +          } | 
|  | 87 | +): string { | 
|  | 88 | +    switch (spec.tag) { | 
|  | 89 | +        case 'user': | 
|  | 90 | +            return `user:${spec.user}`; | 
|  | 91 | +        case 'url': | 
|  | 92 | +            return `url:${spec.hostname}`; | 
|  | 93 | +        case 'space': | 
|  | 94 | +            return `space:${spec.space}`; | 
|  | 95 | +        case 'change-request': | 
|  | 96 | +            return `space:${spec.space}:change-request:${spec.changeRequest}`; | 
|  | 97 | +        case 'revision': | 
|  | 98 | +            return `space:${spec.space}:revision:${spec.revision}`; | 
|  | 99 | +        case 'document': | 
|  | 100 | +            return `space:${spec.space}:document:${spec.document}`; | 
|  | 101 | +        case 'computed-document': | 
|  | 102 | +            return `space:${spec.space}:computed-document:${spec.integration}`; | 
|  | 103 | +        case 'site': | 
|  | 104 | +            return `site:${spec.site}`; | 
|  | 105 | +        case 'integration': | 
|  | 106 | +            return `integration:${spec.integration}`; | 
|  | 107 | +        case 'openapi': | 
|  | 108 | +            return `organization:${spec.organization}:openapi:${spec.openAPISpec}`; | 
|  | 109 | +        default: | 
|  | 110 | +            assertNever(spec); | 
|  | 111 | +    } | 
|  | 112 | +} | 
|  | 113 | + | 
|  | 114 | +/** | 
|  | 115 | + * Get the tags for a computed content source. | 
|  | 116 | + */ | 
|  | 117 | +export function getComputedContentSourceCacheTags( | 
|  | 118 | +    inContext: { | 
|  | 119 | +        spaceId: string; | 
|  | 120 | +        organizationId: string; | 
|  | 121 | +    }, | 
|  | 122 | +    source: ComputedContentSource | 
|  | 123 | +) { | 
|  | 124 | +    const tags: string[] = []; | 
|  | 125 | + | 
|  | 126 | +    // We add the dependencies as tags, to ensure that the computed content is invalidated | 
|  | 127 | +    // when the dependencies are updated. | 
|  | 128 | +    const dependencies = Object.values(source.dependencies ?? {}); | 
|  | 129 | +    if (dependencies.length > 0) { | 
|  | 130 | +        dependencies.forEach((dependency) => { | 
|  | 131 | +            switch (dependency.ref.kind) { | 
|  | 132 | +                case 'space': | 
|  | 133 | +                    tags.push( | 
|  | 134 | +                        getCacheTag({ | 
|  | 135 | +                            tag: 'space', | 
|  | 136 | +                            space: dependency.ref.space, | 
|  | 137 | +                        }) | 
|  | 138 | +                    ); | 
|  | 139 | +                    break; | 
|  | 140 | +                case 'openapi': | 
|  | 141 | +                    tags.push( | 
|  | 142 | +                        getCacheTag({ | 
|  | 143 | +                            tag: 'openapi', | 
|  | 144 | +                            organization: inContext.organizationId, | 
|  | 145 | +                            openAPISpec: dependency.ref.spec, | 
|  | 146 | +                        }) | 
|  | 147 | +                    ); | 
|  | 148 | +                    break; | 
|  | 149 | +                default: | 
|  | 150 | +                    // Do not throw for unknown dependency types | 
|  | 151 | +                    // as it might mean we are lacking behind the API version | 
|  | 152 | +                    break; | 
|  | 153 | +            } | 
|  | 154 | +        }); | 
|  | 155 | +    } else { | 
|  | 156 | +        // Push a dummy tag, as the v1 is only using the first tag | 
|  | 157 | +        tags.push( | 
|  | 158 | +            getCacheTag({ | 
|  | 159 | +                tag: 'computed-document', | 
|  | 160 | +                space: inContext.spaceId, | 
|  | 161 | +                integration: source.integration, | 
|  | 162 | +            }) | 
|  | 163 | +        ); | 
|  | 164 | +    } | 
|  | 165 | + | 
|  | 166 | +    // We invalidate the computed content when a new version of the integration is deployed. | 
|  | 167 | +    tags.push( | 
|  | 168 | +        getCacheTag({ | 
|  | 169 | +            tag: 'integration', | 
|  | 170 | +            integration: source.integration, | 
|  | 171 | +        }) | 
|  | 172 | +    ); | 
|  | 173 | + | 
|  | 174 | +    return tags; | 
|  | 175 | +} | 
0 commit comments