@@ -3,9 +3,9 @@ import fsp from 'fs/promises';
33import { GENERATORS , capitalize , createClientName , exists , toAbsolutePath } from '../common.js' ;
44import type { Language } from '../types.js' ;
55
6- import type { CodeSamples , SnippetForMethod , SnippetSamples } from './types.js' ;
6+ import type { CodeSamples , OpenAPICodeSample , SampleForOperation } from './types.js' ;
77
8- export function getCodeSampleLabel ( language : Language ) : CodeSamples [ 'label' ] {
8+ export function getCodeSampleLabel ( language : Language ) : OpenAPICodeSample [ 'label' ] {
99 switch ( language ) {
1010 case 'csharp' :
1111 return 'C#' ;
@@ -14,14 +14,14 @@ export function getCodeSampleLabel(language: Language): CodeSamples['label'] {
1414 case 'php' :
1515 return 'PHP' ;
1616 default :
17- return capitalize ( language ) as CodeSamples [ 'label' ] ;
17+ return capitalize ( language ) as OpenAPICodeSample [ 'label' ] ;
1818 }
1919}
2020
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 ) ) {
2525 if ( operation === 'import' ) {
2626 continue ;
2727 }
@@ -37,25 +37,25 @@ export function transformCodeSamplesToGuideMethods(snippetSamples: SnippetSample
3737 const initLine = sampleMatch [ 1 ] ;
3838 const callLine = sampleMatch [ 3 ] ;
3939
40- if ( ! ( 'init' in snippetSamples [ language ] ) ) {
41- snippetSamples [ language ] . init = {
40+ if ( ! ( 'init' in codeSamples [ language ] ) ) {
41+ codeSamples [ language ] . init = {
4242 default : initLine . trim ( ) ,
4343 } ;
4444 }
4545
46- snippetSamples [ language ] [ operation ] [ sampleName ] = callLine . trim ( ) ;
46+ codeSamples [ language ] [ operation ] [ sampleName ] = callLine . trim ( ) ;
4747 }
4848 }
4949 }
5050
51- return JSON . stringify ( snippetSamples , null , 2 ) ;
51+ await fsp . writeFile ( toAbsolutePath ( `docs/bundled/ ${ clientName } -snippets.json` ) , JSON . stringify ( codeSamples , null , 2 ) ) ;
5252}
5353
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 > (
5757 ( prev , curr ) => ( { ...prev , [ curr . language ] : { } } ) ,
58- { } as SnippetSamples ,
58+ { } as CodeSamples ,
5959 ) ;
6060
6161 for ( const gen of Object . values ( GENERATORS ) ) {
@@ -76,7 +76,7 @@ export async function transformSnippetsToCodeSamples(clientName: string): Promis
7676
7777 const importMatch = snippetFileContent . match ( / > I M P O R T \n ( [ \s \S ] * ?) \n .* I M P O R T < / ) ;
7878 if ( importMatch ) {
79- snippetSamples [ gen . language ] . import = {
79+ codeSamples [ gen . language ] . import = {
8080 default : importMatch [ 1 ] . trim ( ) ,
8181 } ;
8282 }
@@ -91,24 +91,24 @@ export async function transformSnippetsToCodeSamples(clientName: string): Promis
9191 const operationId = match [ 1 ] ;
9292 const testName = match [ 2 ] || 'default' ;
9393
94- if ( ! snippetSamples [ gen . language ] [ operationId ] ) {
95- snippetSamples [ gen . language ] [ operationId ] = { } ;
94+ if ( ! codeSamples [ gen . language ] [ operationId ] ) {
95+ codeSamples [ gen . language ] [ operationId ] = { } ;
9696 }
9797
98- const snippetForMethod : SnippetForMethod = snippetSamples [ gen . language ] [ operationId ] ;
98+ const sampleForOperation : SampleForOperation = codeSamples [ gen . language ] [ operationId ] ;
9999
100- snippetForMethod [ testName ] = '' ;
100+ sampleForOperation [ testName ] = '' ;
101101
102102 const indent = lines [ 0 ] . length - lines [ 0 ] . trim ( ) . length ;
103103 // skip first and last lines because they contain the SEPARATOR or operationId
104104 lines . forEach ( ( line ) => {
105105 // best effort to determine how far the snippet is indented so we
106106 // can have every snippets in the documentation on the far left
107107 // without impacting the formatting
108- snippetForMethod [ testName ] += `${ line . slice ( indent ) . replaceAll ( / \t / g, ' ' ) } \n` ;
108+ sampleForOperation [ testName ] += `${ line . slice ( indent ) . replaceAll ( / \t / g, ' ' ) } \n` ;
109109 } ) ;
110110 }
111111 }
112112
113- return snippetSamples ;
113+ return codeSamples ;
114114}
0 commit comments