@@ -26,6 +26,7 @@ import setWith from 'lodash.setwith';
2626import semver from 'semver' ;
2727import { IndentationText , Project , QuoteKind , ScriptTarget , VariableDeclarationKind } from 'ts-morph' ;
2828
29+ import { buildCodeSnippetForOperation , getSuggestedOperation } from '../../../lib/suggestedOperations.js' ;
2930import logger from '../../../logger.js' ;
3031import { PACKAGE_VERSION } from '../../../packageInfo.js' ;
3132import Storage from '../../../storage.js' ;
@@ -213,7 +214,7 @@ export default class TSGenerator extends CodeGenerator {
213214 this . createGitIgnore ( ) ;
214215 this . createPackageJSON ( ) ;
215216 this . createTSConfig ( ) ;
216- this . createREADME ( ) ;
217+ await this . createREADME ( ) ;
217218
218219 if ( Object . keys ( this . schemas ) . length ) {
219220 this . createSchemasFile ( srcDirectory ) ;
@@ -260,6 +261,23 @@ export default class TSGenerator extends CodeGenerator {
260261 ] . reduce ( ( prev , next ) => Object . assign ( prev , next ) ) ;
261262 }
262263
264+ async getExampleCodeSnippet ( ) {
265+ // if we've already built the code snippet, return it instead of re-building it!
266+ if ( typeof this . exampleCodeSnippet !== 'undefined' ) {
267+ return this . exampleCodeSnippet ;
268+ }
269+
270+ const operation = getSuggestedOperation ( this . spec ) ;
271+ if ( ! operation ) {
272+ this . exampleCodeSnippet = false ;
273+ return false ;
274+ }
275+
276+ const snippet = await buildCodeSnippetForOperation ( this . spec , operation , { identifier : this . identifier } ) ;
277+ this . exampleCodeSnippet = snippet ;
278+ return snippet ;
279+ }
280+
263281 /**
264282 * Create our main SDK source file.
265283 *
@@ -641,15 +659,28 @@ dist/
641659 * Create a placeholder `README.md` file in the repository, with information on how to use/administer the SDK.
642660 *
643661 */
644- createREADME ( ) {
662+ async createREADME ( ) {
645663 let createdAt = new Date ( ) . toISOString ( ) ;
646664 const currentAPI = Storage . getLockfile ( ) . apis . find ( api => api . identifier === this . identifier ) ;
647665 if ( currentAPI ) createdAt = currentAPI . createdAt ;
666+
667+ let exampleUsage = 'Add SDK setup information and usage examples here so your users get started in a jiffy! 🚀' ;
668+ const exampleSnippet = await this . getExampleCodeSnippet ( ) ;
669+ if ( exampleSnippet ) {
670+ exampleUsage = `
671+ ## Example Usage 🚀
672+
673+ \`\`\`js
674+ ${ exampleSnippet }
675+ \`\`\`
676+ ` . trim ( ) ;
677+ }
678+
648679 const file = `# \`@api/${ this . identifier } \`
649680
650681This SDK was autogenerated by the [\`api\` SDK generator](https://api.readme.dev), powered by [ReadMe](https://readme.com) 🦉
651682
652- Add SDK setup information and usage examples here so your users get started in a jiffy! 🚀
683+ ${ exampleUsage }
653684
654685<!---
655686
0 commit comments