Skip to content

Commit

Permalink
fixes and cleanup of refactor
Browse files Browse the repository at this point in the history
Signed-off-by: karthik2804 <karthik.ganeshram@fermyon.com>
  • Loading branch information
karthik2804 committed Apr 14, 2023
1 parent 89ab899 commit db80273
Show file tree
Hide file tree
Showing 26 changed files with 77 additions and 326 deletions.
3 changes: 0 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ target/release/spinjs: target/wasm32-wasi/release/spin-js-engine.wasm
cd crates/spin-js-cli && \
SPIN_JS_ENGINE_PATH=../../target/wasm32-wasi/release/spin_js_engine.wasm \
cargo build --release $(BUILD_TARGET)
echo "import \"./modules/overrides\"" >> crates/spin-js-engine/src/js_sdk/dist/sdk.d.ts
cp crates/spin-js-engine/src/js_sdk/dist/sdk.d.ts types/lib/index.d.ts
cp -r crates/spin-js-engine/src/js_sdk/dist/modules types/lib/

target/wasm32-wasi/release/spin-js-engine.wasm: crates/spin-js-engine/sdk.ts crates/spin-js-engine/src/lib.rs
cd crates/spin-js-engine && \
Expand Down
6 changes: 4 additions & 2 deletions crates/spin-js-engine/src/js_sdk/modules/spinSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function encodeBody(body: ArrayBuffer | Uint8Array | string) {
/** @internal */
function fetch(uri: string | URL, options?: FetchOptions) {
let encodedBodyData = (options && options.body) ? encodeBody(options.body) : new Uint8Array().buffer
const { status, headers, body } = spinSdk.http.send({
const { status, headers, body } = __internal__.spin_sdk.http.send({
method: (options && options.method) || "GET",
uri: (uri instanceof URL) ? uri.toString() : uri,
headers: (options && options.headers) || {},
Expand Down Expand Up @@ -164,7 +164,9 @@ const spinInternal = {
}

declare global {
const spinSdk: SpinSDK
const __internal__: {
spin_sdk: SpinSDK
}
}

/** @internal */
Expand Down
1 change: 1 addition & 0 deletions examples/typescript/sdk-poc-ts/spin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ version = "1.0.0"
[[component]]
id = "hello"
source = "target/spin-http-js.wasm"
allowed_http_hosts = ["https://some-random-api.ml/"]
[component.trigger]
route = "/..."
[component.config]
Expand Down
3 changes: 3 additions & 0 deletions examples/typescript/sdk-poc-ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ router.get("/home", () => console.log("got home"))
export const handleRequest: HandleRequest = async function (request: HttpRequest): Promise<HttpResponse> {
await router.handleRequest(request)

const dogFact = await fetch("https://some-random-api.ml/facts/dog")
console.log(await dogFact.text())

console.log("The config is ", spinSdk.config.get("test"))

return {
Expand Down
2 changes: 1 addition & 1 deletion spin-sdk/lib/modules/handlerFunction.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ declare class ResponseBuilder {
status(status: number): this;
body(data: ArrayBuffer | Uint8Array | string): this;
}
export { HandleRequest, Handler, HttpRequest, HttpResponse };
export { ResponseBuilder, HandleRequest, Handler, HttpRequest, HttpResponse };
2 changes: 1 addition & 1 deletion spin-sdk/lib/modules/handlerFunction.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {};
export { ResponseBuilder };
30 changes: 25 additions & 5 deletions spin-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
{
"name": "spin-sdk",
"version": "1.0.0",
"name": "@fermyon/spin-sdk",
"version": "0.5.0",
"description": "",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"author": {
"name": "fermyon",
"email": "engineering@fermyon.com",
"url": "https://fermyon.com"
},
"license": "Apache-2.0",
"keywords": [
"fermyon",
"spin",
"sdk",
"spin-js-sdk",
"spin-ts-sdk"
],
"repository": {
"type": "git",
"url": "git+https://github.com/fermyon/spin-js-sdk.git"
},
"bugs": {
"url": "https://github.com/fermyon/spin-js-sdk/issues"
},
"publishConfig": {
"access": "public"
},
"scripts": {
"prepare": "npm run build",
"build": "tsc",
Expand All @@ -12,9 +35,6 @@
"files": [
"lib"
],
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/typedarray-to-buffer": "^4.0.0",
"typedoc": "^0.23.28",
Expand Down
4 changes: 2 additions & 2 deletions spin-sdk/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HandleRequest, Handler, HttpRequest, HttpResponse } from "./modules/handlerFunction";
import { spinSdk, SpinSdK } from "./modules/spinSdk";
import { spinSdk, SpinSdk } from "./modules/spinSdk";
import "./modules/globalDefinitions"

export {HandleRequest, Handler, HttpRequest, HttpResponse, spinSdk, SpinSdK}
export { HandleRequest, Handler, HttpRequest, HttpResponse, spinSdk, SpinSdk }
8 changes: 4 additions & 4 deletions spin-sdk/src/modules/spinSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface KvStore {
* It has methods to access databases like redis, kv
* It also provides utility functions and a router
*/
interface SpinSdK {
interface SpinSdk {
utils: {
toBuffer: (arg0: ArrayBufferView) => Buffer
}
Expand Down Expand Up @@ -55,18 +55,18 @@ interface SpinSdK {

declare global {
const __internal__: {
spin_sdk: SpinSdK
spin_sdk: SpinSdk
}
}


/**
* Sdk module that provides access to spin features
*/
const spinSdk: SpinSdK = __internal__.spin_sdk
const spinSdk: SpinSdk = __internal__.spin_sdk
spinSdk.utils = utils
spinSdk.Router = () => {
return router()
}

export { spinSdk, SpinSdK }
export { spinSdk, SpinSdk }
38 changes: 33 additions & 5 deletions test/test-app/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 test/test-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
"author": "",
"license": "ISC",
"devDependencies": {
"spin-sdk": "file:../../types",
"ts-loader": "^9.4.1",
"typescript": "^4.8.4",
"webpack": "^5.74.0",
"webpack-cli": "^4.10.0"
},
"dependencies": {
"spin-sdk": "file:../../spin-sdk",
"itty-router": "^3.0.11"
}
}
6 changes: 2 additions & 4 deletions test/test-app/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { HandleRequest, HttpRequest, HttpResponse } from "spin-sdk"
import { HandleRequest, HttpRequest, HttpResponse, spinSdk } from "spin-sdk"
import { health, headersTest, outboundHttp, fileRead, dirRead, testFunctionality } from "./test"

const encoder = new TextEncoder()
const decoder = new TextDecoder()
const router = utils.Router()
const router = spinSdk.Router()

// Add paths for router
router.get("/", testFunctionality)
Expand Down
20 changes: 0 additions & 20 deletions types/README.md

This file was deleted.

7 changes: 0 additions & 7 deletions types/lib/index.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion types/lib/modules/crypto.d.ts

This file was deleted.

24 changes: 0 additions & 24 deletions types/lib/modules/fsPromises.d.ts

This file was deleted.

4 changes: 0 additions & 4 deletions types/lib/modules/glob.d.ts

This file was deleted.

67 changes: 0 additions & 67 deletions types/lib/modules/overrides.d.ts

This file was deleted.

Empty file removed types/lib/modules/random.d.ts
Empty file.
Loading

0 comments on commit db80273

Please sign in to comment.