Skip to content

Commit 68187ea

Browse files
committed
Get Contents API
1 parent e8f18c4 commit 68187ea

File tree

8 files changed

+2237
-0
lines changed

8 files changed

+2237
-0
lines changed

Diff for: samples/method/.eslintrc.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module.exports = {
2+
env: {
3+
browser: true,
4+
es2021: true,
5+
},
6+
extends: [
7+
'airbnb-base',
8+
'prettier',
9+
],
10+
parser: '@typescript-eslint/parser',
11+
parserOptions: {
12+
ecmaVersion: 'latest',
13+
sourceType: 'module',
14+
},
15+
plugins: [
16+
'@typescript-eslint',
17+
],
18+
rules: {
19+
},
20+
};

Diff for: samples/method/.prettierrc.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
singleQuote: true,
3+
trailingComma: 'es5',
4+
printWidth: 100,
5+
semi: false,
6+
}

Diff for: samples/method/docs/reference/tutorial-api.yaml

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
openapi: 3.0.0
2+
x-stoplight:
3+
id: 70he5bttrcegx
4+
info:
5+
title: tutorial-api
6+
version: '1.0'
7+
servers:
8+
- url: 'http://localhost:3000'
9+
paths:
10+
/contents:
11+
post:
12+
summary: Create New Content
13+
operationId: post-content
14+
responses:
15+
'200':
16+
description: Content Created
17+
content:
18+
application/json:
19+
schema:
20+
type: array
21+
items:
22+
$ref: '#/components/schemas/Content'
23+
examples: {}
24+
requestBody:
25+
content:
26+
application/json:
27+
schema:
28+
$ref: '#/components/schemas/PostContentRequest'
29+
examples: {}
30+
description: Post the necessary fields for the API to create a new content.
31+
description: Create a new content.
32+
parameters: []
33+
get:
34+
summary: Get Contents
35+
operationId: get-contents
36+
responses:
37+
'200':
38+
description: OK
39+
content:
40+
application/json:
41+
schema:
42+
type: array
43+
items:
44+
$ref: '#/components/schemas/Content'
45+
tags:
46+
- Content
47+
'/contents/{contentsId}':
48+
parameters:
49+
- schema:
50+
type: string
51+
name: contentsId
52+
in: path
53+
required: true
54+
get:
55+
summary: Get Content Info by Content ID
56+
tags:
57+
- Content
58+
responses:
59+
'200':
60+
description: Content Found
61+
content:
62+
application/json:
63+
schema:
64+
$ref: '#/components/schemas/Content'
65+
examples:
66+
Get Content Alice Smith:
67+
value:
68+
id: 142
69+
firstName: Alice
70+
lastName: Smith
71+
72+
dateOfBirth: '1997-10-31'
73+
emailVerified: true
74+
signUpDate: '2019-08-24'
75+
operationId: get-contents-contentId
76+
description: Retrieve the information of the content with the matching content ID.
77+
delete:
78+
summary: Delete a Content by Content ID
79+
operationId: delete-contents-contentsId
80+
responses:
81+
'204':
82+
description: No Content
83+
tags:
84+
- Content
85+
put:
86+
summary: Update a Content
87+
operationId: put-contents-contentsId
88+
responses:
89+
'200':
90+
description: OK
91+
content:
92+
application/json:
93+
schema:
94+
$ref: '#/components/schemas/Content'
95+
tags:
96+
- Content
97+
components:
98+
schemas:
99+
Content:
100+
title: Content
101+
type: object
102+
description: ''
103+
x-examples:
104+
Alice Smith:
105+
id: 142
106+
firstName: Alice
107+
lastName: Smith
108+
109+
dateOfBirth: '1997-10-31'
110+
emailVerified: true
111+
signUpDate: '2019-08-24'
112+
properties:
113+
id:
114+
type: integer
115+
description: Unique identifier for the given content.
116+
title:
117+
type: string
118+
body:
119+
type: string
120+
createdAt:
121+
type: string
122+
format: date
123+
description: The date that the content was created.
124+
updatedAt:
125+
type: string
126+
required:
127+
- id
128+
- title
129+
- body
130+
PostContentRequest:
131+
title: PostContentRequest
132+
x-stoplight:
133+
id: uyyane08s9ozp
134+
type: object
135+
properties:
136+
title:
137+
type: string
138+
body:
139+
type: string
140+
requestBodies: {}

Diff for: samples/method/package.json

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "hello-world",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"scripts": {
7+
"start": "ts-node index.ts",
8+
"lint": "eslint --fix './src/**/*.{js,jsx,ts,tsx}'",
9+
"format": "prettier --write ./src"
10+
},
11+
"dependencies": {
12+
"@typescript-eslint/eslint-plugin": "^5.32.0",
13+
"@typescript-eslint/parser": "^5.32.0",
14+
"eslint": "^7.32.0 || ^8.2.0",
15+
"express": "^4.18.1"
16+
},
17+
"devDependencies": {
18+
"@types/express": "^4.17.13",
19+
"@types/node": "^18.6.4",
20+
"eslint-config-airbnb-base": "^15.0.0",
21+
"eslint-config-prettier": "^8.5.0",
22+
"eslint-plugin-import": "^2.25.2",
23+
"prettier": "^2.7.1",
24+
"ts-node": "^10.9.1",
25+
"typescript": "^4.7.4"
26+
}
27+
}

Diff for: samples/method/src/constants.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export const contents = [
2+
{
3+
id: 1,
4+
title: '坊っちゃん',
5+
body: '親譲りの無鉄砲で小供の時から損ばかりしている。小学校に居る時分学校の二階から飛び降りて一週間ほど腰を抜かした事がある。なぜそんな無闇をしたと聞く人があるかも知れぬ。別段深い理由でもない。新築の二階から首を出していたら、同級生の一人が冗談に、いくら威張っても、そこから飛び降りる事は出来まい。弱虫やーい。と囃したからである。小使に負ぶさって帰って来た時、おやじが大きな眼をして二階ぐらいから飛び降りて腰を抜かす奴があるかと云ったから、この次は抜かさずに飛んで見せますと答えた。\n\n親譲りの無鉄砲で小供の時から損ばかりしている。小学校に居る時分学校の二階から飛び降りて一週間ほど腰を抜かした事がある。なぜそんな無闇をしたと聞く人があるかも知れぬ。別段深い理由でもない。新築の二階から首を出していたら、同級生の一人が冗談に、いくら威張っても、そこから飛び降りる事は出来まい。弱虫やーい。と囃したからである。小使に負ぶさって帰って来た時、おやじが大きな眼をして二階ぐらいから飛び降りて腰を抜かす奴があるかと云ったから、この次は抜かさずに飛んで見せますと答えた。',
6+
createdAt: new Date(),
7+
updatedAt: new Date(),
8+
},
9+
{
10+
id: 2,
11+
title: '方丈記',
12+
body: '行く川のながれは絶えずして、しかも本の水にあらず。よどみに浮ぶうたかたは、かつ消えかつ結びて久しくとゞまることなし。世の中にある人とすみかと、またかくの如し。玉しきの都の中にむねをならべいらかをあらそへる、たかきいやしき人のすまひは、代々を經て盡きせぬものなれど、これをまことかと尋ぬれば、昔ありし家はまれなり。或はこぞ破れ(やけイ)てことしは造り、あるは大家ほろびて小家となる。住む人もこれにおなじ。所もかはらず、人も多かれど、いにしへ見し人は、二三十人が中に、わづかにひとりふたりなり。あしたに死し、ゆふべに生るゝならひ、たゞ水の泡にぞ似たりける。知らず、生れ死ぬる人、いづかたより來りて、いづかたへか去る。又知らず、かりのやどり、誰が爲に心を惱まし、何によりてか目をよろこばしむる。そのあるじとすみかと、無常をあらそひ去るさま、いはゞ朝顏の露にことならず。或は露おちて花のこれり。のこるといへども朝日に枯れぬ。或は花はしぼみて、露なほ消えず。消えずといへども、ゆふべを待つことなし。』およそ物の心を知れりしよりこのかた、四十あまりの春秋をおくれる間に、世のふしぎを見ることやゝたびたびになりぬ。いにし安元三年四月廿八日かとよ、風烈しく吹きてしづかならざりし夜、戌の時ばかり、都のたつみより火出で來りていぬゐに至る。はてには朱雀門、大極殿、大學寮、民部の省まで移りて、ひとよがほどに、塵灰となりにき。火本は樋口富の小路とかや、病人を宿せるかりやより出で來けるとなむ。吹きまよふ風にとかく移り行くほどに、扇をひろげたるが如くすゑひろになりぬ。遠き家は煙にむせび、近きあたりはひたすらほのほを地に吹きつけたり。空には灰を吹きたてたれば、火の光に映じてあまねくくれなゐなる中に、風に堪へず吹き切られたるほのほ、飛ぶが如くにして一二町を越えつゝ移り行く。その中の人うつゝ(しイ)心ならむや。あるひは煙にむせびてたふれ伏し、或は炎にまぐれてたちまちに死しぬ。或は又わづかに身一つからくして遁れたれども、資財を取り出づるに及ばず。七珍萬寳、さながら灰燼となりにき。そのつひえいくそばくぞ。このたび公卿の家十六燒けたり。ましてその外は數を知らず。すべて都のうち、三分が二(一イ)に及べりとぞ。男女死ぬるもの數千人、馬牛のたぐひ邊際を知らず。人のいとなみみなおろかなる中に、さしも危き京中の家を作るとて寶をつひやし心をなやますことは、すぐれてあぢきなくぞ侍るべき。』また治承四年卯月廿九日のころ、中の御門京極のほどより、大なるつじかぜ起りて、六條わたりまで、いかめしく吹きけること侍りき。三四町をかけて吹きまくるに、その中にこもれる家ども、大なるもちひさきも、一つとしてやぶれざるはなし。さながらひらにたふれたるもあり。',
13+
createdAt: new Date(),
14+
updatedAt: new Date(),
15+
},
16+
]

Diff for: samples/method/src/index.ts

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import express from 'express'
2+
import { contents } from './constants'
3+
4+
const app: express.Express = express()
5+
6+
// CORSの許可
7+
app.use((req, res, next) => {
8+
res.header('Access-Control-Allow-Origin', '*')
9+
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
10+
next()
11+
})
12+
13+
// body-parserに基づいた着信リクエストの解析
14+
app.use(express.json())
15+
app.use(express.urlencoded({ extended: true }))
16+
17+
// Get /contents
18+
const router: express.Router = express.Router()
19+
router.get('/contents', (req: express.Request, res: express.Response) => {
20+
res.send(contents)
21+
})
22+
app.use(router)
23+
24+
// 3000番ポートでAPIサーバ起動
25+
app.listen(3000, () => {
26+
console.log('Example app listening on port 3000!')
27+
})

0 commit comments

Comments
 (0)