Skip to content

Commit 581cb5c

Browse files
authored
Merge branch 'master' into master
2 parents 368d879 + 420ca04 commit 581cb5c

File tree

6 files changed

+64
-3731
lines changed

6 files changed

+64
-3731
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ notes/
2121
.deps_check
2222
test/specmap/data/private
2323
browser
24+
package-lock.json

README.md

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ import Swagger from 'swagger-client'
3737
const Swagger = require('swagger-client')
3838
```
3939

40+
##### Import in browser
41+
42+
```html
43+
<script src='browser/swagger-client.js' type='text/javascript'></script>
44+
<script>
45+
var swaggerClient = new SwaggerClient(specUrl);
46+
</script>
47+
```
48+
49+
4050
#### API
4151

4252
This lib exposes these functionalities:
@@ -164,8 +174,8 @@ A client for operations. We're currently using the `apis[tag][operationId]:Execu
164174
165175
OperationId's are meant to be unique within spec, if they're not we do the following:
166176
- If a tag is absent, we use `default` as the internal tag
167-
- If an operationId is missing, we deduce it from the http method and path, i.e. `${method}-${path}`
168-
- If an operationId is duplicated across all operationIds of the spec, we suffix it with \_%d
177+
- If an operationId is missing, we deduce it from the http method and path, i.e. `${method}${path}`, with non-alphanumeric characters escaped to `_`. See these tests ([1](https://github.com/swagger-api/swagger-js/blob/7da5755fa18791cd114ecfc9587dcd1b5c58ede1/test/helpers.js#L7), [2](https://github.com/swagger-api/swagger-js/blob/7da5755fa18791cd114ecfc9587dcd1b5c58ede1/test/helpers.js#L77)) for examples.
178+
- If an operationId is duplicated across all operationIds of the spec, we rename all of them with numbers after the ID to keep them unique. You should not rely on this, as the renaming is non-deterministic. See [this test](https://github.com/swagger-api/swagger-js/blob/7da5755fa18791cd114ecfc9587dcd1b5c58ede1/test/helpers.js#L127) for an example.
169179
170180
```js
171181
Swagger({...}).then((client) => {
@@ -177,6 +187,44 @@ Swagger({...}).then((client) => {
177187
})
178188
```
179189
190+
In Browser
191+
----------
192+
193+
Prepare swagger-client.js by `npm run build-bundle`
194+
Note, browser version exports class `SwaggerClient` to global namespace
195+
If you need activate CORS requests, just enable it by `withCredentials` property at `http`
196+
197+
```html
198+
<html>
199+
<head>
200+
<script src='browser/swagger-client.js' type='text/javascript'></script>
201+
<script>
202+
var specUrl = 'http://petstore.swagger.io/v2/swagger.json'; // data urls are OK too 'data:application/json;base64,abc...'
203+
var swaggerClient = new SwaggerClient(specUrl)
204+
.then(function (swaggerClient) {
205+
swaggerClient.http.withCredentials = true; // this activates CORS, if necessary
206+
207+
return swaggerClient.apis.pet.addPet({id: 1, name: "bobby"}); // chaining promises
208+
}, function (reason) {
209+
console.error("failed to load the spec" + reason);
210+
})
211+
.then(function(addPetResult) {
212+
console.log(addPetResult.obj);
213+
// you may return more promises, if necessary
214+
}, function (reason) {
215+
console.error("failed on API call " + reason);
216+
});
217+
})
218+
</script>
219+
</head>
220+
<body>
221+
check console in browser's dev. tools
222+
</body>
223+
</html>
224+
225+
```
226+
227+
180228
Compatibility
181229
-------------
182230
@@ -220,10 +268,11 @@ As such we've left the static version of `http` to not perform any serialization
220268

221269
```sh
222270
npm install
223-
npm run test # run test
224-
npm run test:watch # run test with change watching
225-
npm run lint # run lint
226-
npm run build # package to release
271+
npm run test # run test
272+
npm run test:watch # run test with change watching
273+
npm run lint # run lint
274+
npm run build # package to release
275+
npm run build-bundle # build browser version available at .../browser
227276
```
228277

229278
# Migration from 2.x

0 commit comments

Comments
 (0)