Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
Ingénierie des modèles
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Baviere Merlin
Ingénierie des modèles
Commits
892dacb4
Commit
892dacb4
authored
2 months ago
by
Baviere Merlin
Browse files
Options
Downloads
Patches
Plain Diff
Cas d'étude Theodo
parent
3e09782b
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
OpenAPI.py
+157
-0
157 additions, 0 deletions
OpenAPI.py
with
157 additions
and
0 deletions
OpenAPI.py
0 → 100644
+
157
−
0
View file @
892dacb4
import
{
mkdir
,
writeFile
}
from
"
fs/promises
"
;
import
openapi
from
"
../openapi.json
"
assert
{
type
:
'
json
'
};
import
{
type
}
from
"
os
"
;
import
{
types
}
from
"
util
"
;
const
targetDirectory
=
"
src/lib/spotify/model
"
;
async
function
generateSpotifyClient
()
{
console
.
log
(
"
\n
Launched generate-spotify-client script
"
);
console
.
log
(
'
Generating Spotify client from OpenApi spec file...
\n
'
)
await
mkdir
(
targetDirectory
,
{
recursive
:
true
});
//
Generate
target
directory
const
schemas
=
openapi
.
components
.
schemas
;
const
typesToGenerate
=
Object
.
keys
(
schemas
);
for
(
const
typeName
of
typesToGenerate
)
{
const
typeSchema
=
schemas
[
typeName
];
generateType
(
typeName
,
typeSchema
);
}
}
function
generateType
(
typeName
,
typeSchema
)
{
console
.
log
(
`Generating type ${typeName}...`
);
const
generatedCode
=
getGeneratedCode
(
typeName
,
typeSchema
);
writeFile
(
`${targetDirectory}/${typeName}.ts`
,
generatedCode
);
}
function
getGeneratedCode
(
typeName
,
typeSchema
)
{
var
libs
=
[]
const
generatedType
=
getGeneratedType
(
typeSchema
,
libs
);
var
importStr
=
""
if
(
libs
!=
[]){
for
(
const
lib
of
libs
){
importStr
+=
'
import {
'
+
lib
+
'
} from
"
./
'
+
lib
+
'"
;
\n
'
}
importStr
+=
'
\n
'
}
return
importStr
+
`export type ${typeName} = ${generatedType};`
;
}
function
getGeneratedType
(
typeSchema
,
libs
,
isRequired
=
true
)
{
if
(
typeSchema
.
allOf
)
{
const
schemaAllOf
=
typeSchema
.
allOf
.
map
(
part
=>
{
if
(
part
[
"
$ref
"
])
{
const
lib
=
part
[
"
$ref
"
].
split
(
"
/
"
).
at
(
-
1
);
if
(
!
libs
.
includes
(
lib
))
{
libs
.
push
(
lib
);
}
return
lib
;
}
return
getGeneratedType
(
part
,
libs
);
});
return
schemaAllOf
.
join
(
"
&
"
);
}
const
schemaType
=
typeSchema
.
type
;
switch
(
schemaType
)
{
case
"
number
"
:
return
`number`
;
case
"
integer
"
:
return
`number`
;
case
"
string
"
:
if
(
typeSchema
.
enum
)
{
const
enumValues
=
typeSchema
.
enum
.
map
(
value
=>
`"${value}"`
).
join
(
"
|
"
);
return
`${enumValues}`
;
}
return
`string`
;
case
"
boolean
"
:
return
`boolean`
;
case
"
array
"
:
if
(
typeSchema
.
items
)
{
if
(
typeSchema
.
items
[
"
oneOf
"
])
{
const
oneOfTypes
=
typeSchema
.
items
[
"
oneOf
"
]
.
map
(
option
=>
{
if
(
option
[
"
$ref
"
])
{
const
lib
=
option
[
"
$ref
"
].
split
(
"
/
"
).
at
(
-
1
);
if
(
!
libs
.
includes
(
lib
))
{
libs
.
push
(
lib
);
}
return
lib
;
}
return
getGeneratedType
(
option
,
libs
);
})
.
join
(
"
|
"
);
return
`(${oneOfTypes})[]`
;
}
else
if
(
typeSchema
.
items
[
"
$ref
"
])
{
const
lib
=
typeSchema
.
items
[
"
$ref
"
].
split
(
"
/
"
).
at
(
-
1
);
if
(
!
libs
.
includes
(
lib
))
{
libs
.
push
(
lib
);
}
return
`${lib}[]`
;
}
else
{
const
itemsType
=
getGeneratedType
(
typeSchema
.
items
,
libs
);
return
`${itemsType}[]`
;
}
}
return
`any[]`
;
case
"
object
"
:
const
requiredList
=
typeSchema
.
required
||
[];
let
res
=
"
{
"
;
if
(
typeSchema
.
properties
)
{
for
(
const
key
of
Object
.
keys
(
typeSchema
.
properties
))
{
const
isPropRequired
=
requiredList
.
includes
(
key
);
const
propertySchema
=
typeSchema
.
properties
[
key
];
if
(
propertySchema
[
"
oneOf
"
])
{
const
oneOfTypes
=
propertySchema
[
"
oneOf
"
]
.
map
(
option
=>
{
if
(
option
[
"
$ref
"
])
{
const
lib
=
option
[
"
$ref
"
].
split
(
"
/
"
).
at
(
-
1
);
if
(
!
libs
.
includes
(
lib
))
{
libs
.
push
(
lib
);
}
return
lib
;
}
return
getGeneratedType
(
option
,
libs
);
})
.
join
(
"
|
"
);
res
+=
isPropRequired
?
`\n ${key}: (${oneOfTypes});`
:
`\n ${key}?: (${oneOfTypes});`
;
}
else
if
(
propertySchema
[
"
$ref
"
])
{
const
lib
=
propertySchema
[
"
$ref
"
].
split
(
"
/
"
).
at
(
-
1
);
if
(
!
libs
.
includes
(
lib
))
{
libs
.
push
(
lib
);
}
res
+=
isPropRequired
?
`\n ${key}: ${lib};`
:
`\n ${key}?: ${lib};`
;
}
else
if
(
propertySchema
.
allOf
)
{
const
combinedType
=
getGeneratedType
(
propertySchema
,
libs
);
res
+=
isPropRequired
?
`\n ${key}: ${combinedType};`
:
`\n ${key}?: ${combinedType};`
;
}
else
{
const
propertyType
=
getGeneratedType
(
propertySchema
,
libs
,
isPropRequired
);
res
+=
isPropRequired
?
`\n ${key}: ${propertyType};`
:
`\n ${key}?: ${propertyType};`
;
}
}
}
res
+=
"
\n
}
"
;
return
res
;
default
:
return
``
;
}
}
generateSpotifyClient
();
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment