-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(oas-to-snippet): mock out
httpsnippet-client-api
behaviors (#961
) ## 🧰 Changes Both `oas-to-snippet` and [httpsnippet-client-api](https://npm.im/httpsnippet-client-api) depending on `oas` has always made it difficult to do breaking releases of `oas` because after publishing `oas`, which automatically bumps `oas-to-snippet`, I then need to bump `httpsnippet-client-api` and _then_ pull that back into `oas-to-snippet`. Nuts to all that. Since we are only loading it in tests to test out behavior for [api](https://npm.im/api) code snippets we can instead just mock out plugin examples and test around that.
- Loading branch information
Showing
7 changed files
with
110 additions
and
56 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* This is a plugin that lightly mimics the behavior of `httpsnippet-client-api` and is used to | ||
* test behaviors related to surfacing snippets from it over stock `node` options. | ||
* | ||
* Why not just use `httpsnippet-client-api`? Because it depends on `oas` we end up with a bit of | ||
* a circular dependency where if we make breaking changes to `oas`, which would update | ||
* `oas-to-snippet` we then need to update `httpsnippet-client-api` to match. If we don't do this | ||
* then the `httpsnippet-client-api` and `oas` dependencies within `oas-to-snippet` get into a | ||
* weird state within NPM where there become conflicts and NPM is unable to load the right one. | ||
* | ||
*/ | ||
import type { Client, ClientPlugin } from '@readme/httpsnippet/targets'; | ||
import type { OASDocument } from 'oas/types'; | ||
|
||
import { CodeBuilder } from '@readme/httpsnippet/helpers/code-builder'; | ||
|
||
interface APIOptions { | ||
api?: { | ||
definition: OASDocument; | ||
|
||
/** | ||
* The string to identify this SDK as. This is used in the `import sdk from '<identifier>'` | ||
* sample as well as the the variable name we attach the SDK to. | ||
* | ||
* @example `@api/developers` | ||
*/ | ||
identifier?: string; | ||
|
||
/** | ||
* The URI that is used to download this API definition from `npx api install`. | ||
* | ||
* @example `@developers/v2.0#17273l2glm9fq4l5` | ||
*/ | ||
registryURI: string; | ||
}; | ||
escapeBrackets?: boolean; | ||
indent?: string | false; | ||
} | ||
|
||
const client: Client<APIOptions> = { | ||
info: { | ||
key: 'api', | ||
title: 'API', | ||
link: 'https://npm.im/api', | ||
description: 'Automatic SDK generation from an OpenAPI definition.', | ||
extname: '.js', | ||
installation: 'npx api install "{packageName}"', | ||
}, | ||
convert: (request, options) => { | ||
const { blank, push, join } = new CodeBuilder({ indent: options?.indent || ' ' }); | ||
|
||
push("console.log('example `api` plugin code snippet.')"); | ||
if (options?.api?.identifier) { | ||
push(`const ${options.api.identifier} = 'example variable name';`); | ||
} | ||
blank(); | ||
|
||
return join(); | ||
}, | ||
}; | ||
|
||
const plugin: ClientPlugin<APIOptions> = { | ||
target: 'node', | ||
client, | ||
}; | ||
|
||
export default plugin; |
27 changes: 27 additions & 0 deletions
27
packages/oas-to-snippet/test/__fixtures__/pluginFailure.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import type { Client, ClientPlugin } from '@readme/httpsnippet/targets'; | ||
|
||
interface APIOptions { | ||
escapeBrackets?: boolean; | ||
indent?: string | false; | ||
} | ||
|
||
const client: Client<APIOptions> = { | ||
info: { | ||
key: 'api', | ||
title: 'API', | ||
link: 'https://npm.im/api', | ||
description: 'Automatic SDK generation from an OpenAPI definition.', | ||
extname: '.js', | ||
installation: 'npx api install "{packageName}"', | ||
}, | ||
convert: () => { | ||
throw new Error('This plugin is expected to fail.'); | ||
}, | ||
}; | ||
|
||
const plugin: ClientPlugin<APIOptions> = { | ||
target: 'node', | ||
client, | ||
}; | ||
|
||
export default plugin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters