Skip to content

Commit 72fd3d3

Browse files
committed
ref to be more extensible
WIP WIP wip fixes [getsentry/action-github-commit] Auto commit expandable parameters? [getsentry/action-github-commit] Auto commit font size?
1 parent bf9b31c commit 72fd3d3

File tree

7 files changed

+324
-132
lines changed

7 files changed

+324
-132
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
---
2+
title: APIs
3+
description: "Learn more about APIs of the SDK."
4+
customCanonicalTag: "/platforms/javascript/apis/"
5+
sidebar_order: 3
6+
---
7+
8+
This page shows all available to-level APIs of the SDK. You can use them like this:
9+
10+
```javascript
11+
import * as Sentry from "@sentry/browser";
12+
13+
Sentry.setTag("tag", "value");
14+
```
15+
16+
## Available Options
17+
18+
<TableOfContents ignoreIds={["available-options"]} />
19+
20+
## Enriching Events
21+
22+
<SdkApi
23+
name="addBreadcrumb"
24+
signature="function addBreadcrumb(breadcrumb: Breadcrumb, hint?: Hint): void"
25+
parameters={[
26+
{
27+
name: "breadcrumb",
28+
required: true,
29+
type: [
30+
{
31+
name: "message",
32+
type: "string",
33+
description:
34+
"If a message is provided, it is rendered as text with all whitespace preserved.",
35+
},
36+
{
37+
name: "type",
38+
type: '"default" | "debug" | "error" | "info" | "navigation" | "http" | "query" | "ui" | "user"',
39+
defaultValue: '"default"',
40+
description:
41+
"The type influences how a breadcrumb is rendered in Sentry. When in doubt, leave it at `default`.",
42+
},
43+
{
44+
name: "level",
45+
type: '"fatal" | "error" | "warning" | "log" | "info" | "debug"',
46+
defaultValue: '"info"',
47+
description:
48+
"The level is used in the UI to emphasize or deemphasize the breadcrumb.",
49+
},
50+
{
51+
name: "category",
52+
type: "string",
53+
description:
54+
"Typically it is a module name or a descriptive string. For instance, `ui.click` could be used to indicate that a click happened",
55+
},
56+
{
57+
name: "data",
58+
type: "Record<string, unknown>",
59+
description:
60+
"Additional data that should be sent with the breadcrumb.",
61+
},
62+
],
63+
},
64+
{
65+
name: "hint",
66+
type: "Record<string, unknown>",
67+
description:
68+
"A hint object containing additional information about the breadcrumb.",
69+
},
70+
]}
71+
>
72+
You can manually add breadcrumbs whenever something interesting happens. For
73+
example, you might manually record a breadcrumb if the user authenticates or
74+
another state change occurs.
75+
</SdkApi>

src/components/sdkApi.tsx

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import {Fragment} from 'react';
2+
import {jsx, jsxs} from 'react/jsx-runtime';
3+
import {toJsxRuntime} from 'hast-util-to-jsx-runtime';
4+
import {Nodes} from 'hastscript/lib/create-h';
5+
import bash from 'refractor/lang/bash.js';
6+
import json from 'refractor/lang/json.js';
7+
import {refractor} from 'refractor/lib/core.js';
8+
9+
import {PlatformCategory} from 'sentry-docs/types';
10+
11+
import {Expandable} from './expandable';
12+
import {SdkDefinition, SdkDefinitionTable} from './sdkDefinition';
13+
14+
interface ParameterDef {
15+
name: string;
16+
type: string | ParameterDef[];
17+
defaultValue?: string;
18+
description?: string;
19+
required?: boolean;
20+
}
21+
22+
type Props = {
23+
name: string;
24+
parameters: ParameterDef[];
25+
signature: string;
26+
categorySupported?: PlatformCategory[];
27+
children?: React.ReactNode;
28+
language?: string;
29+
};
30+
31+
refractor.register(bash);
32+
refractor.register(json);
33+
34+
const codeToJsx = (code: string, lang = 'json') => {
35+
return toJsxRuntime(refractor.highlight(code, lang) as Nodes, {Fragment, jsx, jsxs});
36+
};
37+
38+
export function SdkApi({
39+
name,
40+
children,
41+
signature,
42+
parameters = [],
43+
// TODO: How to handle this default better?
44+
language = 'typescript',
45+
categorySupported = [],
46+
}: Props) {
47+
return (
48+
<SdkDefinition name={name} categorySupported={categorySupported}>
49+
<pre className="mt-2 mb-2">{codeToJsx(signature, language)}</pre>
50+
51+
{parameters.length ? (
52+
<Expandable title="Parameters">
53+
<SdkDefinitionTable className="bg-white">
54+
{parameters.map(param => (
55+
<ApiParameterDef key={param.name} {...param} />
56+
))}
57+
</SdkDefinitionTable>
58+
</Expandable>
59+
) : null}
60+
61+
{children}
62+
</SdkDefinition>
63+
);
64+
}
65+
66+
function ApiParameterDef({name, type, description, required}: ParameterDef) {
67+
return (
68+
<tr>
69+
<th>
70+
{name}
71+
{required ? <span className="text-red">*</span> : null}
72+
</th>
73+
<td>
74+
{typeof type === 'string' ? (
75+
<code>{type}</code>
76+
) : (
77+
<RenderNestedObject objProps={type} />
78+
)}
79+
80+
{description ? <p className="m-0">{description}</p> : null}
81+
</td>
82+
</tr>
83+
);
84+
}
85+
86+
function RenderNestedObject({objProps}: {objProps: ParameterDef[]}) {
87+
return (
88+
<Fragment>
89+
<div>
90+
<code>Object:</code>
91+
</div>
92+
<SdkDefinitionTable className="mt-1">
93+
{objProps.map(prop => (
94+
<ApiParameterDef key={prop.name} {...prop} />
95+
))}
96+
</SdkDefinitionTable>
97+
</Fragment>
98+
);
99+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import {PlatformCategory} from 'sentry-docs/types';
2+
3+
import styles from './style.module.scss';
4+
5+
import {PlatformCategorySection} from '../platformCategorySection';
6+
7+
type Props = {
8+
name: string;
9+
categorySupported?: PlatformCategory[];
10+
children?: React.ReactNode;
11+
};
12+
13+
export function SdkDefinition({name, children, categorySupported = []}: Props) {
14+
return (
15+
<PlatformCategorySection supported={categorySupported}>
16+
<div>
17+
<h3 id={name} aria-label={name} data-sdk-option>
18+
<a href={`#${name}`}>
19+
<svg
20+
className="anchorlink before"
21+
viewBox="0 0 24 24"
22+
xmlns="http://www.w3.org/2000/svg"
23+
width="16"
24+
height="16"
25+
>
26+
<path
27+
d="M9.199 13.599a5.99 5.99 0 0 0 3.949 2.345 5.987 5.987 0 0 0 5.105-1.702l2.995-2.994a5.992 5.992 0 0 0 1.695-4.285 5.976 5.976 0 0 0-1.831-4.211 5.99 5.99 0 0 0-6.431-1.242 6.003 6.003 0 0 0-1.905 1.24l-1.731 1.721a.999.999 0 1 0 1.41 1.418l1.709-1.699a3.985 3.985 0 0 1 2.761-1.123 3.975 3.975 0 0 1 2.799 1.122 3.997 3.997 0 0 1 .111 5.644l-3.005 3.006a3.982 3.982 0 0 1-3.395 1.126 3.987 3.987 0 0 1-2.632-1.563A1 1 0 0 0 9.201 13.6zm5.602-3.198a5.99 5.99 0 0 0-3.949-2.345 5.987 5.987 0 0 0-5.105 1.702l-2.995 2.994a5.992 5.992 0 0 0-1.695 4.285 5.976 5.976 0 0 0 1.831 4.211 5.99 5.99 0 0 0 6.431 1.242 6.003 6.003 0 0 0 1.905-1.24l1.723-1.723a.999.999 0 1 0-1.414-1.414L9.836 19.81a3.985 3.985 0 0 1-2.761 1.123 3.975 3.975 0 0 1-2.799-1.122 3.997 3.997 0 0 1-.111-5.644l3.005-3.006a3.982 3.982 0 0 1 3.395-1.126 3.987 3.987 0 0 1 2.632 1.563 1 1 0 0 0 1.602-1.198z"
28+
fill="currentColor"
29+
/>
30+
</svg>
31+
{name}
32+
</a>
33+
</h3>
34+
35+
{children}
36+
</div>
37+
</PlatformCategorySection>
38+
);
39+
}
40+
41+
export function SdkDefinitionTable({
42+
children,
43+
className,
44+
}: {
45+
children?: React.ReactNode;
46+
className?: string;
47+
}) {
48+
return (
49+
<table className={styles['sdk-option-table'] + (className ? ` ${className}` : '')}>
50+
<tbody>{children}</tbody>
51+
</table>
52+
);
53+
}

src/components/sdkOption/style.module.scss renamed to src/components/sdkDefinition/style.module.scss

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,22 @@
88

99
.sdk-option-table {
1010
--border-radius: 6px;
11+
font-size: 1rem;
1112
margin-top: 1rem;
12-
1313
width: auto !important;
1414

1515
// This is necessary to make rounded borders work :(
1616
border-collapse: separate !important;
1717
border-spacing: 0;
1818

19+
&:first-child {
20+
margin-top: 0;
21+
}
22+
23+
&:last-child {
24+
margin-bottom: 0;
25+
}
26+
1927
& tr th, & tr td {
2028
border-bottom-width: 0;
2129
border-right-width: 0;

src/components/sdkOption.tsx

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import {getCurrentPlatformOrGuide} from 'sentry-docs/docTree';
2+
import {serverContext} from 'sentry-docs/serverContext';
3+
import {PlatformCategory} from 'sentry-docs/types';
4+
5+
import {PlatformCategorySection} from './platformCategorySection';
6+
import {PlatformSection} from './platformSection';
7+
import {SdkDefinition, SdkDefinitionTable} from './sdkDefinition';
8+
9+
type Props = {
10+
name: string;
11+
type: string;
12+
categorySupported?: PlatformCategory[];
13+
children?: React.ReactNode;
14+
defaultValue?: string;
15+
envVar?: string;
16+
};
17+
18+
export function SdkOption({
19+
name,
20+
children,
21+
type,
22+
defaultValue,
23+
envVar,
24+
categorySupported = [],
25+
}: Props) {
26+
const {showBrowserOnly, showServerLikeOnly} = getPlatformHints(categorySupported);
27+
28+
return (
29+
<SdkDefinition name={name} categorySupported={categorySupported}>
30+
<SdkDefinitionTable>
31+
{type && <OptionDefRow label="Type" value={type} />}
32+
{defaultValue && <OptionDefRow label="Default" value={defaultValue} />}
33+
34+
<PlatformCategorySection supported={['server', 'serverless']}>
35+
<PlatformSection notSupported={['javascript.nextjs']}>
36+
{envVar && <OptionDefRow label="ENV Variable" value={envVar} />}
37+
</PlatformSection>
38+
</PlatformCategorySection>
39+
40+
{showBrowserOnly && <OptionDefRow label="Only available on" value="Client" />}
41+
{showServerLikeOnly && <OptionDefRow label="Only available on" value="Server" />}
42+
</SdkDefinitionTable>
43+
44+
{children}
45+
</SdkDefinition>
46+
);
47+
}
48+
49+
function OptionDefRow({label, value}: {label: string; value: string}) {
50+
return (
51+
<tr>
52+
<th>{label}</th>
53+
<td>
54+
<code>{value}</code>
55+
</td>
56+
</tr>
57+
);
58+
}
59+
60+
function getPlatformHints(categorySupported: PlatformCategory[]) {
61+
const {rootNode, path} = serverContext();
62+
const currentPlatformOrGuide = getCurrentPlatformOrGuide(rootNode, path);
63+
const currentCategories = currentPlatformOrGuide?.categories || [];
64+
65+
// We only handle browser, server & serverless here for now
66+
const currentIsBrowser = currentCategories.includes('browser');
67+
const currentIsServer = currentCategories.includes('server');
68+
const currentIsServerless = currentCategories.includes('serverless');
69+
const currentIsServerLike = currentIsServer || currentIsServerless;
70+
71+
const hasCategorySupported = categorySupported.length > 0;
72+
const supportedBrowserOnly =
73+
categorySupported.includes('browser') &&
74+
!categorySupported.includes('server') &&
75+
!categorySupported.includes('serverless');
76+
const supportedServerLikeOnly =
77+
!categorySupported.includes('browser') &&
78+
(categorySupported.includes('server') || categorySupported.includes('serverless'));
79+
80+
const showBrowserOnly =
81+
hasCategorySupported && supportedBrowserOnly && currentIsServerLike;
82+
const showServerLikeOnly =
83+
hasCategorySupported && supportedServerLikeOnly && currentIsBrowser;
84+
85+
return {showBrowserOnly, showServerLikeOnly};
86+
}

0 commit comments

Comments
 (0)