Skip to content

Commit

Permalink
Repo lint and typing improvements (#73)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
valentin0h authored Oct 12, 2022
1 parent d762a7c commit 63fe46a
Show file tree
Hide file tree
Showing 29 changed files with 391 additions and 1,778 deletions.
6 changes: 3 additions & 3 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"printWidth": 100,
"singleQuote": true,
"tabWidth": 4
"printWidth": 100,
"singleQuote": true,
"tabWidth": 4
}
3 changes: 3 additions & 0 deletions integrations/contentkit/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["@gitbook/eslint-config/integration"]
}
10 changes: 7 additions & 3 deletions integrations/contentkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
"version": "0.0.0",
"private": true,
"dependencies": {
"@gitbook/api": "*",
"@gitbook/runtime": "*"
},
"devDependencies": {
"@gitbook/cli": "*"
"@gitbook/eslint-config": "*",
"@gitbook/tsconfig": "*"
},
"scripts": {}
"scripts": {
"lint": "eslint ./**/*.ts*",
"typecheck": "tsc --noEmit",
"publish-integrations-staging": "gitbook publish ."
}
}
3 changes: 3 additions & 0 deletions integrations/contentkit/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@gitbook/tsconfig/integration.json"
}
3 changes: 3 additions & 0 deletions integrations/figma/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["@gitbook/eslint-config/integration"]
}
8 changes: 5 additions & 3 deletions integrations/figma/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
"version": "0.0.0",
"private": true,
"dependencies": {
"@gitbook/api": "*",
"@gitbook/runtime": "*"
},
"devDependencies": {
"@gitbook/cli": "*"
"@gitbook/eslint-config": "*",
"@gitbook/tsconfig": "*"
},
"scripts": {
"lint": "eslint ./**/*.ts*",
"typecheck": "tsc --noEmit",
"publish-integrations-staging": "gitbook publish ."
}
}
}
46 changes: 31 additions & 15 deletions integrations/figma/src/figma.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FigmaRuntimeContext } from "./types";
import { FigmaRuntimeContext } from './types';

export interface FileNodeId {
fileId: string;
Expand All @@ -17,16 +17,16 @@ interface FigmaAPINodes extends FigmaAPIFile {
[key: string]: {
document?: {
name?: string;
}
}
}
};
};
};
}

// https://www.figma.com/developers/api#get-images-endpoint
interface FigmaAPIImages {
images: {
[key: string]: string;
}
};
}

/**
Expand Down Expand Up @@ -55,17 +55,21 @@ export function extractNodeFromURL(input: string): FileNodeId | undefined {
/**
* Fetch the informations about a Figma node.
*/
export async function fetchFigmaNode(fileId: string, nodeId: string, context: FigmaRuntimeContext) {
export async function fetchFigmaNode(fileId: string, nodeId: string, context: FigmaRuntimeContext) {
try {
const [node, image] = await Promise.all([
fetchFigmaAPI<FigmaAPINodes>(`files/${fileId}/nodes`, { ids: nodeId, depth: 1 }, context),
fetchFigmaImage(fileId, nodeId, context)
fetchFigmaAPI<FigmaAPINodes>(
`files/${fileId}/nodes`,
{ ids: nodeId, depth: 1 },
context
),
fetchFigmaImage(fileId, nodeId, context),
]);
return {
name: node.name,
thumbnailUrl: node.thumbnailUrl,
nodeName: node.nodes[nodeId]?.document?.name,
nodeImage: image
nodeImage: image,
};
} catch (err) {
return undefined;
Expand All @@ -75,9 +79,17 @@ export function extractNodeFromURL(input: string): FileNodeId | undefined {
/**
* Fetch the preview image for a node.
*/
export async function fetchFigmaImage(fileId: string, nodeId: string, context: FigmaRuntimeContext) {
export async function fetchFigmaImage(
fileId: string,
nodeId: string,
context: FigmaRuntimeContext
) {
try {
const image = await fetchFigmaAPI<FigmaAPIImages>(`images/${fileId}`, { ids: nodeId, format: 'svg' }, context);
const image = await fetchFigmaAPI<FigmaAPIImages>(
`images/${fileId}`,
{ ids: nodeId, format: 'svg' },
context
);
const imageUrl = image.images?.[nodeId];
if (!imageUrl) {
return undefined;
Expand All @@ -86,12 +98,12 @@ export function extractNodeFromURL(input: string): FileNodeId | undefined {
const response = await fetch(imageUrl);
const svgText = await response.text();

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

return {
width: parseInt(width, 10),
height: parseInt(height, 10),
url: imageUrl
url: imageUrl,
};
} catch (err) {
return undefined;
Expand All @@ -113,7 +125,11 @@ export async function fetchFigmaFile(fileId: string, context: FigmaRuntimeContex
/**
* Execute a Figma API request.
*/
export async function fetchFigmaAPI<T>(path: string, params: object, { environment }: FigmaRuntimeContext): Promise<T> {
export async function fetchFigmaAPI<T>(
path: string,
params: object,
{ environment }: FigmaRuntimeContext
): Promise<T> {
const url = new URL(`https://api.figma.com/v1/${path}`);

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

const response = await fetch(url.toString(), {
headers: {
'Authorization': `Bearer ${environment.installation.configuration.oauth_credentials.access_token}`,
Authorization: `Bearer ${environment.installation.configuration.oauth_credentials.access_token}`,
},
});

Expand Down
1 change: 1 addition & 0 deletions integrations/figma/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
RuntimeEnvironment,
RuntimeContext,
} from '@gitbook/runtime';

import { extractNodeFromURL, fetchFigmaFile, fetchFigmaNode } from './figma';

interface FigmaInstallationConfiguration {
Expand Down
2 changes: 1 addition & 1 deletion integrations/figma/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RuntimeEnvironment, RuntimeContext } from "@gitbook/runtime";
import { RuntimeEnvironment, RuntimeContext } from '@gitbook/runtime';

export interface FigmaInstallationConfiguration {
oauth_credentials?: {
Expand Down
3 changes: 3 additions & 0 deletions integrations/figma/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@gitbook/tsconfig/integration.json"
}
3 changes: 3 additions & 0 deletions integrations/mermaid/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["@gitbook/eslint-config/integration"]
}
10 changes: 8 additions & 2 deletions integrations/mermaid/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
"@gitbook/runtime": "*"
},
"devDependencies": {
"@gitbook/cli": "*"
"@gitbook/cli": "*",
"@gitbook/eslint-config": "*",
"@gitbook/tsconfig": "*"
},
"scripts": {}
"scripts": {
"lint": "eslint ./**/*.ts*",
"typecheck": "tsc --noEmit",
"publish-integrations-staging": "gitbook publish ."
}
}
68 changes: 35 additions & 33 deletions integrations/mermaid/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,63 +1,65 @@
import { createIntegration, createComponent } from "@gitbook/runtime";
import { createIntegration, createComponent } from '@gitbook/runtime';

const defaultContent =`graph TD
const defaultContent = `graph TD
Mermaid --> Diagram`;

const diagramBlock = createComponent<{
content?: string;
}, {
content: string;
}>({
const diagramBlock = createComponent<
{
content?: string;
},
{
content: string;
}
>({
componentId: 'diagram',
initialState: (props) => {
return {
content: props.content || defaultContent
content: props.content || defaultContent,
};
},
async render(element, { environment }) {
const { editable } = element.context;
const { content } = element.state;

return (
<block>
<box style="secondary">
<vstack>
{editable ? (
<>
<box>
<codeblock
state="content"
content={content}
syntax="mermaid"
onContentChange={{
action: '@editor.node.updateProps',
props: {
content: element.dynamicState('content')
}
}}
<codeblock
state="content"
content={content}
syntax="mermaid"
onContentChange={{
action: '@editor.node.updateProps',
props: {
content: element.dynamicState('content'),
},
}}
/>
</box>
<divider />
</>
) : null}
<box>
<webframe
source={{
url: environment.integration.urls.publicEndpoint,
}}
aspectRatio={16 / 9}
data={{
content: element.dynamicState('content'),
}}
/>
<webframe
source={{
url: environment.integration.urls.publicEndpoint,
}}
aspectRatio={16 / 9}
data={{
content: element.dynamicState('content'),
}}
/>
</box>
</vstack>
</box>
</block>
);
}
})

},
});

export default createIntegration({
events: {
Expand Down Expand Up @@ -123,7 +125,7 @@ export default createIntegration({
},
}
);
}
},
},
components: [diagramBlock]
components: [diagramBlock],
});
3 changes: 3 additions & 0 deletions integrations/mermaid/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@gitbook/tsconfig/integration.json"
}
1 change: 1 addition & 0 deletions integrations/runkit/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/webframe.ts
3 changes: 3 additions & 0 deletions integrations/runkit/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["@gitbook/eslint-config/integration"]
}
4 changes: 4 additions & 0 deletions integrations/runkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@
},
"devDependencies": {
"@gitbook/cli": "*",
"@gitbook/eslint-config": "*",
"@gitbook/tsconfig": "*",
"esbuild": "^0.15.7"
},
"scripts": {
"build": "node -r esbuild-register ./webframe/scripts/build.ts",
"lint": "eslint ./**/*.ts*",
"typecheck": "tsc --noEmit",
"publish-integrations-staging": "gitbook publish ."
}
}
2 changes: 2 additions & 0 deletions integrations/runkit/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Router } from 'itty-router';

import {
createIntegration,
createComponent,
RuntimeContext,
FetchEventCallback,
} from '@gitbook/runtime';

import { fetchRunKitFromLink } from './runkit';
import { webFrameHTML } from './webframe';

Expand Down
8 changes: 5 additions & 3 deletions integrations/segment/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
"@gitbook/runtime": "*"
},
"devDependencies": {
"@gitbook/cli": "*",
"@gitbook/eslint-config": "*",
"@gitbook/tsconfig": "*",
"test": "^3.2.1",
"assert": "^2.0.0"
},
"scripts": {
"lint": "eslint ./src/**/*.ts",
"publish-integrations": "gitbook publish .",
"lint": "eslint ./**/*.ts*",
"typecheck": "tsc --noEmit",
"publish-integrations-staging": "gitbook publish .",
"publish-integrations": "gitbook publish .",
"test": "node -r esbuild-register ./tests/index.ts"
}
}
1 change: 1 addition & 0 deletions integrations/segment/tests/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import test from 'test';

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

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

const fakeSpaceViewEvent: api.SpaceViewEvent = {
Expand Down
2 changes: 1 addition & 1 deletion integrations/segment/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"extends": "@gitbook/tsconfig/integration.json"
}
}
Loading

0 comments on commit 63fe46a

Please sign in to comment.