Skip to content

Commit

Permalink
docs: cubejs signature (#837)
Browse files Browse the repository at this point in the history
  • Loading branch information
vasilev-alex authored Jul 15, 2020
1 parent 6fa1a16 commit 83cea56
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 32 deletions.
28 changes: 28 additions & 0 deletions docs-gen/src/resources/helpers/param-type-to-string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ParameterReflection } from 'typedoc';
import { ReflectionType, UnionType } from 'typedoc/dist/lib/models';

import { signatureTitle } from './signature-title';
import { type } from './type';

export default function paramTypeToString(parameter: ParameterReflection) {
let typeOut;

if (parameter.type instanceof ReflectionType && parameter.type.toString() === 'function') {
const declarations = parameter.type.declaration.signatures?.map((sig) => signatureTitle.call(sig, false, true));
typeOut = declarations.join(' | ').replace(/\n/, '');
} else if (parameter.type instanceof UnionType) {
typeOut = parameter.type.types
.map((currentType) => {
if (currentType instanceof ReflectionType) {
const declarations = currentType.declaration.signatures?.map((sig) => signatureTitle.call(sig, false, true));
return declarations.join(' | ').replace(/\n/, '');
}
return type.call(currentType);
})
.join(' | ');
} else {
typeOut = type.call(parameter.type);
}

return typeOut;
}
16 changes: 4 additions & 12 deletions docs-gen/src/resources/helpers/parameter-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import { ParameterReflection } from 'typedoc';

import MarkdownTheme from '../../theme';
import { stripLineBreaks } from './strip-line-breaks';
import { type } from './type';
import { signatureTitle } from './signature-title';
import { ReflectionType } from 'typedoc/dist/lib/models';
import paramTypeToString from './param-type-to-string';

export function parameterTable(this: ParameterReflection[], hideUncommented: boolean) {
const md = [];
Expand All @@ -25,25 +23,19 @@ export function parameterTable(this: ParameterReflection[], hideUncommented: boo
if (hasComments) {
headers.push('Description');
}

if (hideUncommented && !hasComments) {
return '';
}

if (hideUncommented) {
md.push('**Parameters:**\n');
}

const rows = this.map((parameter) => {
const isOptional = parameter.flags.includes('Optional');

let typeOut;
if (parameter.type instanceof ReflectionType && parameter.type.toString() === 'function') {
const declarations = parameter.type.declaration.signatures?.map((sig) => signatureTitle.call(sig, false, true));
typeOut = declarations.join(' | ').replace(/\n/, '');
} else {
typeOut = type.call(parameter.type);
}
const typeOut = paramTypeToString(parameter);

const row = [
`${parameter.flags.isRest ? '...' : ''}${parameter.name}${isOptional ? '?' : ''}`,
Expand Down
3 changes: 2 additions & 1 deletion docs-gen/src/resources/helpers/signature-title.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { SignatureReflection } from 'typedoc';
import { memberSymbol } from './member-symbol';
import { type } from './type';
import { ReflectionType } from 'typedoc/dist/lib/models';
import paramTypeToString from './param-type-to-string';

export function signatureTitle(this: SignatureReflection, showSymbol: boolean = false) {
const md = [];
Expand Down Expand Up @@ -43,7 +44,7 @@ export function signatureTitle(this: SignatureReflection, showSymbol: boolean =
if (param.flags.isOptional) {
paramsmd.push('?');
}
paramsmd.push(`**: ${type.call(param.type)}`);
paramsmd.push(`**: ${paramTypeToString(param)}`);
return paramsmd.join('');
})
.join(', ')
Expand Down
16 changes: 1 addition & 15 deletions docs-gen/src/resources/helpers/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import {
UnionType,
} from 'typedoc/dist/lib/models/types';

import MarkdownTheme from '../../theme';
import { ReflectionKind } from 'typedoc';
import { LinkPlugin } from '../../plugins/LinkPlugin';

export function type(
Expand Down Expand Up @@ -63,19 +61,7 @@ export function type(
function getReferenceType(model: ReferenceType) {
const md = [];

// let parentName = '';
// if (model.reflection?.kindOf(ReflectionKind.TypeAlias) && !(model.reflection as any).stickToParent) {
// parentName = 'Types';
// }

md.push(
model.reflection ? LinkPlugin.toLink(model.name, model.reflection) : model.name
// model.reflection
// ? `[${model.reflection.name}](${MarkdownTheme.handlebars.helpers.relativeURL(
// LinkPlugin.anchorName(parentName + model.reflection.name)
// )})`
// : model.name
);
md.push(model.reflection ? LinkPlugin.toLink(model.name, model.reflection) : model.name);

if (model.typeArguments) {
md.push(`‹${model.typeArguments.map((typeArgument) => `${type.call(typeArgument)}`).join(', ')}›`);
Expand Down
16 changes: 14 additions & 2 deletions docs/Cube.js-Frontend/@cubejs-client-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Vanilla JavaScript Cube.js client.

## cubejs

**cubejs**(**apiToken**: string, **options**: [CubeJSApiOptions](#types-cube-js-api-options)): *[CubejsApi](#cubejs-api)*
**cubejs**(**apiToken**: string | () => *Promise‹string›*, **options**: [CubeJSApiOptions](#types-cube-js-api-options)): *[CubejsApi](#cubejs-api)*

Creates an instance of the `CubejsApi`. The API entry point.

Expand All @@ -22,13 +22,25 @@ const cubejsApi = cubejs(
);
```

You can also pass an async function or a promise that will resolve to the API token

```js
import cubejs from '@cubejs-client/core';
const cubejsApi = cubejs(
async () => await Auth.getJwtToken(),
{ apiUrl: 'http://localhost:4000/cubejs-api/v1' }
);
```

**Parameters:**

Name | Type | Description |
------ | ------ | ------ |
apiToken | string | [API token](security) is used to authorize requests and determine SQL database you're accessing. In the development mode, Cube.js Backend will print the API token to the console on on startup. Can be an async function without arguments that returns the API token. |
apiToken | string | () => *Promise‹string›* | [API token](security) is used to authorize requests and determine SQL database you're accessing. In the development mode, Cube.js Backend will print the API token to the console on on startup. Can be an async function without arguments that returns the API token. |
options | [CubeJSApiOptions](#types-cube-js-api-options) | - |

**cubejs**(**options**: [CubeJSApiOptions](#types-cube-js-api-options)): *[CubejsApi](#cubejs-api)*

## CubejsApi

Main class for accessing Cube.js API
Expand Down
16 changes: 14 additions & 2 deletions packages/cubejs-client-core/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ declare module '@cubejs-client/core' {
*/
meta(options?: LoadMethodOptions, callback?: LoadMethodCallback<Meta>): void;
}

/**
* Creates an instance of the `CubejsApi`. The API entry point.
*
Expand All @@ -697,8 +697,20 @@ declare module '@cubejs-client/core' {
* { apiUrl: 'http://localhost:4000/cubejs-api/v1' }
* );
* ```
*
* You can also pass an async function or a promise that will resolve to the API token
*
* ```js
* import cubejs from '@cubejs-client/core';
* const cubejsApi = cubejs(
* async () => await Auth.getJwtToken(),
* { apiUrl: 'http://localhost:4000/cubejs-api/v1' }
* );
* ```
*
* @param apiToken - [API token](security) is used to authorize requests and determine SQL database you're accessing. In the development mode, Cube.js Backend will print the API token to the console on on startup. Can be an async function without arguments that returns the API token.
* @order 1
*/
export default function cubejs(apiToken: string, options: CubeJSApiOptions): CubejsApi;
export default function cubejs(apiToken: string | (() => Promise<string>), options: CubeJSApiOptions): CubejsApi;
export default function cubejs(options: CubeJSApiOptions): CubejsApi;
}

0 comments on commit 83cea56

Please sign in to comment.