@@ -3,9 +3,9 @@ import fsp from 'fs/promises';
3
3
import { GENERATORS , capitalize , createClientName , exists , toAbsolutePath } from '../common.js' ;
4
4
import type { Language } from '../types.js' ;
5
5
6
- import type { CodeSamples , SnippetForMethod , SnippetSamples } from './types.js' ;
6
+ import type { CodeSamples , OpenAPICodeSample , SampleForOperation } from './types.js' ;
7
7
8
- export function getCodeSampleLabel ( language : Language ) : CodeSamples [ 'label' ] {
8
+ export function getCodeSampleLabel ( language : Language ) : OpenAPICodeSample [ 'label' ] {
9
9
switch ( language ) {
10
10
case 'csharp' :
11
11
return 'C#' ;
@@ -14,14 +14,14 @@ export function getCodeSampleLabel(language: Language): CodeSamples['label'] {
14
14
case 'php' :
15
15
return 'PHP' ;
16
16
default :
17
- return capitalize ( language ) as CodeSamples [ 'label' ] ;
17
+ return capitalize ( language ) as OpenAPICodeSample [ 'label' ] ;
18
18
}
19
19
}
20
20
21
- // Iterates over the snippet samples and sanitize the data to only keep the method part in order to use it in the guides .
22
- export function transformCodeSamplesToGuideMethods ( snippetSamples : SnippetSamples ) : string {
23
- for ( const [ language , operationWithSample ] of Object . entries ( snippetSamples ) ) {
24
- for ( const [ operation , samples ] of Object . entries ( operationWithSample ) ) {
21
+ // Iterates over the result of `transformSnippetsToCodeSamples` in order to generate a JSON file for the doc to consume .
22
+ export async function bundleCodeSamplesForDoc ( codeSamples : CodeSamples , clientName : string ) : Promise < void > {
23
+ for ( const [ language , operationWithSamples ] of Object . entries ( codeSamples ) ) {
24
+ for ( const [ operation , samples ] of Object . entries ( operationWithSamples ) ) {
25
25
if ( operation === 'import' ) {
26
26
continue ;
27
27
}
@@ -37,25 +37,25 @@ export function transformCodeSamplesToGuideMethods(snippetSamples: SnippetSample
37
37
const initLine = sampleMatch [ 1 ] ;
38
38
const callLine = sampleMatch [ 3 ] ;
39
39
40
- if ( ! ( 'init' in snippetSamples [ language ] ) ) {
41
- snippetSamples [ language ] . init = {
40
+ if ( ! ( 'init' in codeSamples [ language ] ) ) {
41
+ codeSamples [ language ] . init = {
42
42
default : initLine . trim ( ) ,
43
43
} ;
44
44
}
45
45
46
- snippetSamples [ language ] [ operation ] [ sampleName ] = callLine . trim ( ) ;
46
+ codeSamples [ language ] [ operation ] [ sampleName ] = callLine . trim ( ) ;
47
47
}
48
48
}
49
49
}
50
50
51
- return JSON . stringify ( snippetSamples , null , 2 ) ;
51
+ await fsp . writeFile ( toAbsolutePath ( `docs/bundled/ ${ clientName } -snippets.json` ) , JSON . stringify ( codeSamples , null , 2 ) ) ;
52
52
}
53
53
54
- // For a given `clientName`, reads the matching snippet file for every available clients and builds an hashmap of snippets per operationId per language.
55
- export async function transformSnippetsToCodeSamples ( clientName : string ) : Promise < SnippetSamples > {
56
- const snippetSamples = Object . values ( GENERATORS ) . reduce (
54
+ // Reads the generated `docs/snippets/` file for every languages of the given `clientName` and builds an hashmap of snippets per operationId per language.
55
+ export async function transformGeneratedSnippetsToCodeSamples ( clientName : string ) : Promise < CodeSamples > {
56
+ const codeSamples = Object . values ( GENERATORS ) . reduce < CodeSamples > (
57
57
( prev , curr ) => ( { ...prev , [ curr . language ] : { } } ) ,
58
- { } as SnippetSamples ,
58
+ { } as CodeSamples ,
59
59
) ;
60
60
61
61
for ( const gen of Object . values ( GENERATORS ) ) {
@@ -76,7 +76,7 @@ export async function transformSnippetsToCodeSamples(clientName: string): Promis
76
76
77
77
const importMatch = snippetFileContent . match ( / > I M P O R T \n ( [ \s \S ] * ?) \n .* I M P O R T < / ) ;
78
78
if ( importMatch ) {
79
- snippetSamples [ gen . language ] . import = {
79
+ codeSamples [ gen . language ] . import = {
80
80
default : importMatch [ 1 ] . trim ( ) ,
81
81
} ;
82
82
}
@@ -91,24 +91,24 @@ export async function transformSnippetsToCodeSamples(clientName: string): Promis
91
91
const operationId = match [ 1 ] ;
92
92
const testName = match [ 2 ] || 'default' ;
93
93
94
- if ( ! snippetSamples [ gen . language ] [ operationId ] ) {
95
- snippetSamples [ gen . language ] [ operationId ] = { } ;
94
+ if ( ! codeSamples [ gen . language ] [ operationId ] ) {
95
+ codeSamples [ gen . language ] [ operationId ] = { } ;
96
96
}
97
97
98
- const snippetForMethod : SnippetForMethod = snippetSamples [ gen . language ] [ operationId ] ;
98
+ const sampleForOperation : SampleForOperation = codeSamples [ gen . language ] [ operationId ] ;
99
99
100
- snippetForMethod [ testName ] = '' ;
100
+ sampleForOperation [ testName ] = '' ;
101
101
102
102
const indent = lines [ 0 ] . length - lines [ 0 ] . trim ( ) . length ;
103
103
// skip first and last lines because they contain the SEPARATOR or operationId
104
104
lines . forEach ( ( line ) => {
105
105
// best effort to determine how far the snippet is indented so we
106
106
// can have every snippets in the documentation on the far left
107
107
// without impacting the formatting
108
- snippetForMethod [ testName ] += `${ line . slice ( indent ) . replaceAll ( / \t / g, ' ' ) } \n` ;
108
+ sampleForOperation [ testName ] += `${ line . slice ( indent ) . replaceAll ( / \t / g, ' ' ) } \n` ;
109
109
} ) ;
110
110
}
111
111
}
112
112
113
- return snippetSamples ;
113
+ return codeSamples ;
114
114
}
0 commit comments