Skip to content

Commit

Permalink
Remove dependency on OpenAPI and JSONSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
DZakh committed Aug 4, 2024
1 parent 932d4df commit 03d6abf
Show file tree
Hide file tree
Showing 7 changed files with 7 additions and 405 deletions.
142 changes: 0 additions & 142 deletions __tests__/Rest_openapi_test.res

This file was deleted.

44 changes: 5 additions & 39 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@
"res:clean": "rescript clean",
"test": "ava"
},
"dependencies": {
"rescript-openapi": "0.3.x",
"rescript-json-schema": "6.x"
},
"devDependencies": {
"@dzakh/rescript-ava": "3.0.0",
"ava": "5.2.x",
Expand Down
4 changes: 1 addition & 3 deletions rescript.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
},
"suffix": ".res.js",
"bs-dependencies": [
"rescript-schema",
"rescript-openapi",
"rescript-json-schema"
"rescript-schema"
],
"bs-dev-dependencies": ["@dzakh/rescript-ava"]
}
113 changes: 0 additions & 113 deletions src/Rest.res
Original file line number Diff line number Diff line change
Expand Up @@ -525,116 +525,3 @@ let client = (~baseUrl, ~fetcher=ApiFetcher.default, ~jsonQuery=false) => {
jsonQuery,
}
}

let routeToOpenAPI = (route: route<'variables, 'response>, ~jsonQuery=false): OpenAPI.operation => {
let params = route->params
let schemas = (params.variablesSchema->S.classify->Obj.magic)["fields"]

let parameters = []

let paramsSchemaItems: dict<S.item> = switch (schemas["params"]: option<S.item>) {
| None => Js.Dict.empty()
| Some(paramsItem) => (paramsItem.schema->S.classify->Obj.magic)["fields"]
}
params.pathItems->Js.Array2.forEach(pathItem => {
switch pathItem {
| Static(_) => ()
| Param({name}) =>
switch paramsSchemaItems
->Js.Dict.unsafeGet(name)
->Option.unsafeSome {
| None => panic(`Path parameter "${name}" is not defined in variables`)
| Some(fieldItem) =>
parameters
->Js.Array2.push(
OpenAPI.Mutable.WithReference.object(
(
{
name,
in_: Path,
required: true,
schema: switch JSONSchema.make(fieldItem.schema) {
| Ok(schema) => schema
| Error(message) => panic(message)
},
}: OpenAPI.Mutable.parameter
),
),
)
->ignore
}
}
})

let querySchemaItems: array<S.item> = switch (schemas["query"]: option<S.item>) {
| None => []
| Some(queryItem) => (queryItem.schema->S.classify->Obj.magic)["items"]
}
querySchemaItems->Js.Array2.forEach(item => {
let required = ref(Some(true))
let schema = switch item.schema->S.classify {
| Option(s) => {
required := None
s
}
| _ => item.schema
}

let jsonSchema = switch JSONSchema.make(schema) {
| Ok(schema) => schema
| Error(message) => panic(message)
}

let parameter: OpenAPI.Mutable.parameter = {
name: item.location,
in_: Query,
}
switch required.contents {
| Some(required) => parameter.required = Some(required)
| None => ()
}
switch item.schema->S.description {
| Some(description) => parameter.description = Some(description)
| None => ()
}
switch jsonQuery {
| true =>
parameter.content = Some(
Js.Dict.fromArray([
(
"application/json",
OpenAPI.Mutable.WithReference.object(({schema: jsonSchema}: OpenAPI.Mutable.mediaType)),
),
]),
)
| false =>
parameter.schema = Some(jsonSchema)
switch schema->S.classify {
| Object(_) => parameter.style = Some(#deepObject)
| _ => ()
}
}

parameters
->Js.Array2.push(OpenAPI.Mutable.WithReference.object(parameter))
->ignore
})

let operation: OpenAPI.Mutable.operation = {
operationId: %raw(`route.name`),
parameters,
}
switch params.definition.summary {
| Some(summary) => operation.summary = Some(summary)
| None => ()
}
switch params.definition.description {
| Some(description) => operation.description = Some(description)
| None => ()
}
switch params.definition.deprecated {
| Some(deprecated) => operation.deprecated = Some(deprecated)
| None => ()
}
operation->(Obj.magic: OpenAPI.Mutable.operation => OpenAPI.operation)
}
Loading

0 comments on commit 03d6abf

Please sign in to comment.