Skip to content

Commit

Permalink
v0.0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
ryusuke410 committed Jan 8, 2024
1 parent 03b8090 commit f03a4fa
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
26 changes: 21 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
## Spicy Input (Swagger UI Plugin)

There is no straightforward method in the standard features of Swagger UI to programmatically manipulate user inputs such as query parameters and request bodies. Spicy-input provides a concise interface for retrieving, setting, and subscribing to these user inputs.
There is no straightforward method in the standard features of Swagger UI to programmatically manipulate user inputs such as query parameters and request bodies. Spicy Input provides a concise interface for retrieving, setting, and subscribing to these user inputs.

Furthermore, user inputs are completely lost upon reloading Swagger UI. During API development, there is often a desire to reload Swagger UI whenever the API interface is modified, but also a need to remember the inputted content. Spicy-input makes it easy to retain these user inputs.
Furthermore, user inputs are completely lost upon reloading Swagger UI. During API development, there is often a desire to remember the inputted content. Spicy Input makes it easy to retain these user inputs. Swagger UI intelligently saves user inputs based on each API interface, so it works well in most cases even in use cases where multiple specs are dynamically swapped. For the same reason, if the details of the interface, such as the type of input values, are changed, the user inputs are not retained.

## Basic Usage

## With npm
### With npm

```shell
npm i spicy-input
Expand Down Expand Up @@ -36,7 +36,7 @@ SwaggerUI({
})
```

## With UNPKG
### With UNPKG

```html
<link rel="stylesheet" type="text/css" href="https://unpkg.com/swagger-ui-dist/swagger-ui.css" />
Expand All @@ -55,7 +55,7 @@ window.onload = () => {
</script>
```

## With jsDelivr
### With jsDelivr

```html
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/swagger-ui-dist/swagger-ui.css" />
Expand All @@ -76,6 +76,8 @@ window.onload = () => {

## Options

### persistUserInputs

You can choose to activate the memory function for user inputs by specifying options. It is enabled by default.

```typescript
Expand All @@ -89,6 +91,20 @@ SwaggerUI({
})
```

### prefix

By specifying an internal prefix, Spicy Input can separate namespaces for storing various types of data. The default value is `(default)`. This feature is only necessary when deploying the same spec API across multiple environments and viewing them individually through Swagger UI arranged under the same domain for each environment. Normally, there is no need to specify this.

```typescript
import * as spicyInput from 'spicy-input';

SwaggerUI({
plugins: [
spicyInput.getPlugin({ prefix: "my-env" })
]
})
```

## APIs

You can manipulate user inputs through the system.
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "spicy-input",
"version": "0.0.6",
"version": "0.0.7",
"description": "Spicy-input provides a concise interface for retrieving, setting, and subscribing to these user inputs.",
"scripts": {
"build": "tsc -p ./tsconfig.json && tsc -p ./tsconfig.cjs.json && tsconfig-to-dual-package && tsup",
Expand Down
12 changes: 9 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export const fn: (system: SwaggerUiSystem) => SpicyInputFn = (system: SwaggerUiS

export interface SpicyInputOptions {
persistUserInputs?: boolean;
prefix?: string;
}

export const getPlugin: (options?: SpicyInputOptions) => SwaggerUIPlugin = (options?: SpicyInputOptions) => (system: SwaggerUiSystem) => {
Expand All @@ -169,7 +170,7 @@ export const getPlugin: (options?: SpicyInputOptions) => SwaggerUIPlugin = (opti
}

const afterLoad = !persistUserInputs ? undefined : (system: SwaggerUiSystem) => {
const key = getLocalStorageKey("inputs");
const key = getLocalStorageKey(system, "inputs");
const inputs = (() => {
try {
const json = localStorage.getItem(key);
Expand Down Expand Up @@ -314,10 +315,15 @@ export const getPlugin: (options?: SpicyInputOptions) => SwaggerUIPlugin = (opti
};
}
},
spicyInputPrivate: {
options: () => options,
},
},
}
}

const getLocalStorageKey = (key: string) => {
return `spicy-input/${key}`;
const getLocalStorageKey = (system: SwaggerUiSystem, key: string) => {
const options = system.fn.spicyInputPrivate.options();
const prefix = options?.prefix ?? "(default)";
return `spicy-input/${prefix}/${key}`;
};

0 comments on commit f03a4fa

Please sign in to comment.