Skip to content

Commit

Permalink
add request.{path,query,headers,cookies,session}.json()
Browse files Browse the repository at this point in the history
  • Loading branch information
terrablue committed Nov 30, 2023
1 parent 5e12005 commit 42d8c78
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 10 deletions.
3 changes: 2 additions & 1 deletion packages/binding/src/bindings/go/make_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const make_session = session => {
}
},
get: session.get,
stringified: session.toString(),
},
};
};
Expand All @@ -43,7 +44,7 @@ export default request => {
body: JSON.stringify(request.body),
...from(dispatchers.map(property => [
property, {
properties: request[property].toString(),
stringified: request[property].toString(),
...make_dispatcher(request[property]),
},
])),
Expand Down
8 changes: 7 additions & 1 deletion packages/binding/src/bindings/go/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Array []any

type Dispatcher struct {
Get func(string) any
Json func() map[string]any
%%DISPATCH_STRUCT%%
}

Expand Down Expand Up @@ -63,9 +64,10 @@ func make_url(request js.Value) URL {
func make_dispatcher(key string, request js.Value) Dispatcher {
properties := make(map[string]any);
value := request.Get(key)
json.Unmarshal([]byte(value.Get("properties").String()), &properties);
json.Unmarshal([]byte(value.Get("stringified").String()), &properties);

return Dispatcher{
// Get
func(property string) any {
switch properties[property].(type) {
case string:
Expand All @@ -74,6 +76,10 @@ func make_dispatcher(key string, request js.Value) Dispatcher {
return nil;
}
},
// Json
func() map[string]any {
return properties;
},
%%DISPATCH_MAKE%%
};
}
Expand Down
8 changes: 8 additions & 0 deletions packages/binding/src/bindings/go/session.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package main

import "syscall/js"
import "encoding/json"
import "errors"

type Session struct {
Exists func() bool
Get func(string) any
Json func() map[string]any
Set func(string, any) error
Create func(map[string]any)
Destroy func()
}

func make_session(request js.Value) Session {
session := request.Get("session");
properties := make(map[string]any);
json.Unmarshal([]byte(session.Get("stringified").String()), &properties);

return Session{
// Exists
Expand Down Expand Up @@ -42,6 +46,10 @@ func make_session(request js.Value) Session {

return nil;
},
// Json
func() map[string]any {
return properties;
},
// Set
func(key string, value any) error {
r := session.Get("set").Invoke(key, value);
Expand Down
13 changes: 5 additions & 8 deletions packages/binding/src/bindings/python/make_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,11 @@ const wrap_store = store => {
};
};

const wrap_stores = object => {
return to(object).reduce((reduced, [key, value]) => {
if (value.connection !== undefined) {
return { ...reduced, [key]: wrap_store(value) };
}
return { ...reduced, [key]: wrap_stores(value) };
}, {});
};
const is_store = value => value.connection !== undefined;
const wrap_stores = object => to(object).reduce((reduced, [key, value]) => ({
...reduced,
[key]: is_store(value) ? wrap_store(value) : wrap_stores(value),
}), {});

export default request => {
return {
Expand Down
3 changes: 3 additions & 0 deletions packages/primate/src/dispatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export default (patches = {}) => (object, raw, cased = true) => {

return object[cased ? property : property.toLowerCase()];
},
json() {
return JSON.parse(JSON.stringify(object));
},
toString() {
return JSON.stringify(object);
},
Expand Down
3 changes: 3 additions & 0 deletions packages/session/src/make_session.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export default (store, id) => {
store.delete($id);
}
},
json() {
return JSON.parse(JSON.stringify(all()));
},
toString() {
return JSON.stringify(all());
},
Expand Down

0 comments on commit 42d8c78

Please sign in to comment.