Last active
August 23, 2020 03:59
-
-
Save kevinswiber/fd57320bbfe2aab28c45de370aded550 to your computer and use it in GitHub Desktop.
wasm wrapper around postman2openapi
This file contains hidden or 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
[package] | |
name = "postman2openapi-wasm" | |
version = "0.1.0" | |
authors = ["Kevin Swiber <[email protected]>"] | |
edition = "2018" | |
[lib] | |
crate-type = ["cdylib", "rlib"] | |
[features] | |
default = ["console_error_panic_hook"] | |
[dependencies] | |
wasm-bindgen = "0.2.63" | |
postman2openapi = { path = "../postman2openapi" } | |
# The `console_error_panic_hook` crate provides better debugging of panics by | |
# logging them with `console.error`. This is great for development, but requires | |
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for | |
# code size when deploying. | |
console_error_panic_hook = { version = "0.1.6", optional = true } | |
# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size | |
# compared to the default allocator's ~10K. It is slower than the default | |
# allocator, however. | |
# | |
# Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now. | |
wee_alloc = { version = "0.4.5", optional = true } | |
[dev-dependencies] | |
wasm-bindgen-test = "0.3.13" | |
[profile.release] | |
# Tell `rustc` to optimize for small code size. | |
opt-level = "z" | |
lto = true | |
[package.metadata.wasm-pack.profile.release] | |
wasm-opt = ["-Oz", "--enable-mutable-globals"] |
This file contains hidden or 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
import { default as collection } from './collection'; | |
import * as postman2openapi from "postman2openapi-wasm"; | |
const postman = JSON.stringify(collection); | |
const openapi = postman2openapi.from_str(postman, 'yaml'); | |
const textarea = document.createElement('textarea'); | |
textarea.style = "width: 100%; height: 700px;"; | |
textarea.value = openapi; | |
document.body.appendChild(textarea); |
This file contains hidden or 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
use postman2openapi::TargetFormat; | |
use std::result::Result; | |
use std::str::FromStr; | |
use wasm_bindgen::prelude::*; | |
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global | |
// allocator. | |
#[cfg(feature = "wee_alloc")] | |
#[global_allocator] | |
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; | |
#[wasm_bindgen] | |
pub fn from_str(collection: &str, format: &str) -> Result<String, JsValue> { | |
match postman2openapi::from_str( | |
collection, | |
TargetFormat::from_str(format).unwrap_or(TargetFormat::Yaml), | |
) { | |
Ok(val) => Ok(val), | |
Err(err) => Err(JsValue::from_str(&err.to_string())), | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment