Skip to content

Instantly share code, notes, and snippets.

@chenyong
Created June 3, 2019 03:28
Show Gist options
  • Save chenyong/38f94da1330ab221bf48c59f4ae8018b to your computer and use it in GitHub Desktop.
Save chenyong/38f94da1330ab221bf48c59f4ae8018b to your computer and use it in GitHub Desktop.
Comparing Clojure spec and TypeScript with multiple structs
(def data {
:mock-all? false
:mocks [
{:path "api/test/json", :type :json :status 200 :data {"code" 200, "message" "This is a test json message in EDN"}}
{:path "api/test/file", :type :file :status 200 :file "user-info.json"}
{:path "api/test/text", :type :text :status 200 :text "user-info.json"}
]
:sites {
"api" {:target :apis}
"web" {:target :proxy, :path "/web", :host "http://localhost:8200"}
}
})
(s/def ::data some?)
(s/def ::enabled? boolean?)
(s/def ::file string?)
(s/def ::host string?)
(s/def ::mock-all? boolean?)
(defmulti mock-type :type)
(defmethod mock-type :file [_]
(s/keys :req-un [::path ::type ::status ::file] :opt-un [::enabled?]))
(defmethod mock-type :json [_]
(s/keys :req-un [::path ::type ::status ::data] :opt-un [::enabled?]))
(defmethod mock-type :text [_]
(s/keys :req-un [::path ::type ::status ::text] :opt-un [::enabled?]))
(s/def ::mocks (s/coll-of (s/multi-spec mock-type :type)))
(defmulti site-type :target)
(defmethod site-type :apis [_] (s/keys :req-un [::target]))
(defmethod site-type :proxy [_] (s/keys :req-un [::target ::path ::host]))
(s/def ::sites (s/map-of string? (s/and map? (s/multi-spec site-type :target))))
(s/def ::mock-config (s/keys :req-un [::mock-all? ::mocks ::sites]))
(s/def ::path string?)
(s/def ::status number?)
(s/def ::target #{:apis :proxy})
(s/def ::text string?)
(s/def ::type #{:file :json :text})
interface IText {
path: string;
type: "text";
status: number;
text: string;
}
interface IFile {
path: string;
type: "file";
status: number;
file: string;
}
interface IJson {
path: string;
type: "json";
status: number;
data: any;
}
type Mock = IText | IFile | IJson;
interface IConfig {
"mock-all?": boolean;
mocks: Mock[];
sites: { [k: string]: { target: "apis" } | { target: "proxy"; path: string; host: string } };
}
let data: IConfig = {
"mock-all?": false,
mocks: [
{
path: "api/test/json",
type: "json",
status: 200,
data: {
code: 200,
message: "This is a test json message in EDN",
},
},
{
path: "api/test/file",
type: "file",
status: 200,
file: "user-info.json",
},
{
path: "api/test/text",
type: "text",
status: 200,
text: "user-info.json",
},
],
sites: {
api: {
target: "apis",
},
web: {
target: "proxy",
path: "/web",
host: "http://localhost:8200",
},
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment