Skip to content

Instantly share code, notes, and snippets.

@kevinswiber
Last active August 23, 2020 03:59
Show Gist options
  • Save kevinswiber/fd57320bbfe2aab28c45de370aded550 to your computer and use it in GitHub Desktop.
Save kevinswiber/fd57320bbfe2aab28c45de370aded550 to your computer and use it in GitHub Desktop.
wasm wrapper around postman2openapi
[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"]
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);
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