Skip to content

Commit 63fe46a

Browse files
authored
Repo lint and typing improvements (GitbookIO#73)
* add workers types * add node types as dev deps * Fix jsx issues * fix lint * Ignore linting ignored file * Add initial typescript subpath export workaround * Add comments * Add tsconfig and eslint config as dev dependencies * Remove script non production ready integrations * fix package.json syntax issue
1 parent d762a7c commit 63fe46a

29 files changed

+391
-1778
lines changed

.prettierrc.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"printWidth": 100,
3-
"singleQuote": true,
4-
"tabWidth": 4
2+
"printWidth": 100,
3+
"singleQuote": true,
4+
"tabWidth": 4
55
}
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": ["@gitbook/eslint-config/integration"]
3+
}

integrations/contentkit/package.json

+7-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
"version": "0.0.0",
44
"private": true,
55
"dependencies": {
6-
"@gitbook/api": "*",
76
"@gitbook/runtime": "*"
87
},
98
"devDependencies": {
10-
"@gitbook/cli": "*"
9+
"@gitbook/eslint-config": "*",
10+
"@gitbook/tsconfig": "*"
1111
},
12-
"scripts": {}
12+
"scripts": {
13+
"lint": "eslint ./**/*.ts*",
14+
"typecheck": "tsc --noEmit",
15+
"publish-integrations-staging": "gitbook publish ."
16+
}
1317
}

integrations/contentkit/tsconfig.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "@gitbook/tsconfig/integration.json"
3+
}

integrations/figma/.eslintrc.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": ["@gitbook/eslint-config/integration"]
3+
}

integrations/figma/package.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
"version": "0.0.0",
44
"private": true,
55
"dependencies": {
6-
"@gitbook/api": "*",
76
"@gitbook/runtime": "*"
87
},
98
"devDependencies": {
10-
"@gitbook/cli": "*"
9+
"@gitbook/eslint-config": "*",
10+
"@gitbook/tsconfig": "*"
1111
},
1212
"scripts": {
13+
"lint": "eslint ./**/*.ts*",
14+
"typecheck": "tsc --noEmit",
1315
"publish-integrations-staging": "gitbook publish ."
1416
}
15-
}
17+
}

integrations/figma/src/figma.ts

+31-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FigmaRuntimeContext } from "./types";
1+
import { FigmaRuntimeContext } from './types';
22

33
export interface FileNodeId {
44
fileId: string;
@@ -17,16 +17,16 @@ interface FigmaAPINodes extends FigmaAPIFile {
1717
[key: string]: {
1818
document?: {
1919
name?: string;
20-
}
21-
}
22-
}
20+
};
21+
};
22+
};
2323
}
2424

2525
// https://www.figma.com/developers/api#get-images-endpoint
2626
interface FigmaAPIImages {
2727
images: {
2828
[key: string]: string;
29-
}
29+
};
3030
}
3131

3232
/**
@@ -55,17 +55,21 @@ export function extractNodeFromURL(input: string): FileNodeId | undefined {
5555
/**
5656
* Fetch the informations about a Figma node.
5757
*/
58-
export async function fetchFigmaNode(fileId: string, nodeId: string, context: FigmaRuntimeContext) {
58+
export async function fetchFigmaNode(fileId: string, nodeId: string, context: FigmaRuntimeContext) {
5959
try {
6060
const [node, image] = await Promise.all([
61-
fetchFigmaAPI<FigmaAPINodes>(`files/${fileId}/nodes`, { ids: nodeId, depth: 1 }, context),
62-
fetchFigmaImage(fileId, nodeId, context)
61+
fetchFigmaAPI<FigmaAPINodes>(
62+
`files/${fileId}/nodes`,
63+
{ ids: nodeId, depth: 1 },
64+
context
65+
),
66+
fetchFigmaImage(fileId, nodeId, context),
6367
]);
6468
return {
6569
name: node.name,
6670
thumbnailUrl: node.thumbnailUrl,
6771
nodeName: node.nodes[nodeId]?.document?.name,
68-
nodeImage: image
72+
nodeImage: image,
6973
};
7074
} catch (err) {
7175
return undefined;
@@ -75,9 +79,17 @@ export function extractNodeFromURL(input: string): FileNodeId | undefined {
7579
/**
7680
* Fetch the preview image for a node.
7781
*/
78-
export async function fetchFigmaImage(fileId: string, nodeId: string, context: FigmaRuntimeContext) {
82+
export async function fetchFigmaImage(
83+
fileId: string,
84+
nodeId: string,
85+
context: FigmaRuntimeContext
86+
) {
7987
try {
80-
const image = await fetchFigmaAPI<FigmaAPIImages>(`images/${fileId}`, { ids: nodeId, format: 'svg' }, context);
88+
const image = await fetchFigmaAPI<FigmaAPIImages>(
89+
`images/${fileId}`,
90+
{ ids: nodeId, format: 'svg' },
91+
context
92+
);
8193
const imageUrl = image.images?.[nodeId];
8294
if (!imageUrl) {
8395
return undefined;
@@ -86,12 +98,12 @@ export function extractNodeFromURL(input: string): FileNodeId | undefined {
8698
const response = await fetch(imageUrl);
8799
const svgText = await response.text();
88100

89-
const [,width, height] = svgText.match(/viewBox="0 0 (\d+) (\d+)"/) || [];
101+
const [, width, height] = svgText.match(/viewBox="0 0 (\d+) (\d+)"/) || [];
90102

91103
return {
92104
width: parseInt(width, 10),
93105
height: parseInt(height, 10),
94-
url: imageUrl
106+
url: imageUrl,
95107
};
96108
} catch (err) {
97109
return undefined;
@@ -113,7 +125,11 @@ export async function fetchFigmaFile(fileId: string, context: FigmaRuntimeContex
113125
/**
114126
* Execute a Figma API request.
115127
*/
116-
export async function fetchFigmaAPI<T>(path: string, params: object, { environment }: FigmaRuntimeContext): Promise<T> {
128+
export async function fetchFigmaAPI<T>(
129+
path: string,
130+
params: object,
131+
{ environment }: FigmaRuntimeContext
132+
): Promise<T> {
117133
const url = new URL(`https://api.figma.com/v1/${path}`);
118134

119135
Object.entries(params).forEach(([key, value]) => {
@@ -122,7 +138,7 @@ export async function fetchFigmaAPI<T>(path: string, params: object, { environme
122138

123139
const response = await fetch(url.toString(), {
124140
headers: {
125-
'Authorization': `Bearer ${environment.installation.configuration.oauth_credentials.access_token}`,
141+
Authorization: `Bearer ${environment.installation.configuration.oauth_credentials.access_token}`,
126142
},
127143
});
128144

integrations/figma/src/index.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
RuntimeEnvironment,
66
RuntimeContext,
77
} from '@gitbook/runtime';
8+
89
import { extractNodeFromURL, fetchFigmaFile, fetchFigmaNode } from './figma';
910

1011
interface FigmaInstallationConfiguration {

integrations/figma/src/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { RuntimeEnvironment, RuntimeContext } from "@gitbook/runtime";
1+
import { RuntimeEnvironment, RuntimeContext } from '@gitbook/runtime';
22

33
export interface FigmaInstallationConfiguration {
44
oauth_credentials?: {

integrations/figma/tsconfig.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "@gitbook/tsconfig/integration.json"
3+
}

integrations/mermaid/.eslintrc.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": ["@gitbook/eslint-config/integration"]
3+
}

integrations/mermaid/package.json

+8-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
77
"@gitbook/runtime": "*"
88
},
99
"devDependencies": {
10-
"@gitbook/cli": "*"
10+
"@gitbook/cli": "*",
11+
"@gitbook/eslint-config": "*",
12+
"@gitbook/tsconfig": "*"
1113
},
12-
"scripts": {}
14+
"scripts": {
15+
"lint": "eslint ./**/*.ts*",
16+
"typecheck": "tsc --noEmit",
17+
"publish-integrations-staging": "gitbook publish ."
18+
}
1319
}

integrations/mermaid/src/index.tsx

+35-33
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,65 @@
1-
import { createIntegration, createComponent } from "@gitbook/runtime";
1+
import { createIntegration, createComponent } from '@gitbook/runtime';
22

3-
const defaultContent =`graph TD
3+
const defaultContent = `graph TD
44
Mermaid --> Diagram`;
55

6-
const diagramBlock = createComponent<{
7-
content?: string;
8-
}, {
9-
content: string;
10-
}>({
6+
const diagramBlock = createComponent<
7+
{
8+
content?: string;
9+
},
10+
{
11+
content: string;
12+
}
13+
>({
1114
componentId: 'diagram',
1215
initialState: (props) => {
1316
return {
14-
content: props.content || defaultContent
17+
content: props.content || defaultContent,
1518
};
1619
},
1720
async render(element, { environment }) {
1821
const { editable } = element.context;
1922
const { content } = element.state;
20-
23+
2124
return (
2225
<block>
2326
<box style="secondary">
2427
<vstack>
2528
{editable ? (
2629
<>
2730
<box>
28-
<codeblock
29-
state="content"
30-
content={content}
31-
syntax="mermaid"
32-
onContentChange={{
33-
action: '@editor.node.updateProps',
34-
props: {
35-
content: element.dynamicState('content')
36-
}
37-
}}
31+
<codeblock
32+
state="content"
33+
content={content}
34+
syntax="mermaid"
35+
onContentChange={{
36+
action: '@editor.node.updateProps',
37+
props: {
38+
content: element.dynamicState('content'),
39+
},
40+
}}
3841
/>
3942
</box>
4043
<divider />
4144
</>
4245
) : null}
4346
<box>
44-
<webframe
45-
source={{
46-
url: environment.integration.urls.publicEndpoint,
47-
}}
48-
aspectRatio={16 / 9}
49-
data={{
50-
content: element.dynamicState('content'),
51-
}}
52-
/>
47+
<webframe
48+
source={{
49+
url: environment.integration.urls.publicEndpoint,
50+
}}
51+
aspectRatio={16 / 9}
52+
data={{
53+
content: element.dynamicState('content'),
54+
}}
55+
/>
5356
</box>
5457
</vstack>
5558
</box>
5659
</block>
5760
);
58-
}
59-
})
60-
61+
},
62+
});
6163

6264
export default createIntegration({
6365
events: {
@@ -123,7 +125,7 @@ export default createIntegration({
123125
},
124126
}
125127
);
126-
}
128+
},
127129
},
128-
components: [diagramBlock]
130+
components: [diagramBlock],
129131
});

integrations/mermaid/tsconfig.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "@gitbook/tsconfig/integration.json"
3+
}

integrations/runkit/.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
src/webframe.ts

integrations/runkit/.eslintrc.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": ["@gitbook/eslint-config/integration"]
3+
}

integrations/runkit/package.json

+4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@
99
},
1010
"devDependencies": {
1111
"@gitbook/cli": "*",
12+
"@gitbook/eslint-config": "*",
13+
"@gitbook/tsconfig": "*",
1214
"esbuild": "^0.15.7"
1315
},
1416
"scripts": {
1517
"build": "node -r esbuild-register ./webframe/scripts/build.ts",
18+
"lint": "eslint ./**/*.ts*",
19+
"typecheck": "tsc --noEmit",
1620
"publish-integrations-staging": "gitbook publish ."
1721
}
1822
}

integrations/runkit/src/index.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { Router } from 'itty-router';
2+
23
import {
34
createIntegration,
45
createComponent,
56
RuntimeContext,
67
FetchEventCallback,
78
} from '@gitbook/runtime';
9+
810
import { fetchRunKitFromLink } from './runkit';
911
import { webFrameHTML } from './webframe';
1012

integrations/segment/package.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@
77
"@gitbook/runtime": "*"
88
},
99
"devDependencies": {
10-
"@gitbook/cli": "*",
10+
"@gitbook/eslint-config": "*",
11+
"@gitbook/tsconfig": "*",
1112
"test": "^3.2.1",
1213
"assert": "^2.0.0"
1314
},
1415
"scripts": {
15-
"lint": "eslint ./src/**/*.ts",
16-
"publish-integrations": "gitbook publish .",
16+
"lint": "eslint ./**/*.ts*",
17+
"typecheck": "tsc --noEmit",
1718
"publish-integrations-staging": "gitbook publish .",
19+
"publish-integrations": "gitbook publish .",
1820
"test": "node -r esbuild-register ./tests/index.ts"
1921
}
2022
}

integrations/segment/tests/events.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import test from 'test';
33

44
import * as api from '@gitbook/api';
55

6+
// eslint-disable-next-line import/no-internal-modules
67
import { generateSegmentTrackEvent } from '../src/events';
78

89
const fakeSpaceViewEvent: api.SpaceViewEvent = {

integrations/segment/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
22
"extends": "@gitbook/tsconfig/integration.json"
3-
}
3+
}

0 commit comments

Comments
 (0)