-
Notifications
You must be signed in to change notification settings - Fork 2
QueryParameter returns URLSearchParams value #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { PrimitiveType, ObjectType, ArrayType } from "./Types"; | ||
import * as Core from "./Core"; | ||
import { PrimitiveType, ObjectType, ArrayType, ParameterOfForm } from "./Types"; | ||
import * as Guard from "./Guard"; | ||
|
||
export interface Parameter { | ||
value: PrimitiveType | ObjectType | ArrayType; | ||
|
@@ -9,7 +9,36 @@ export interface Parameter { | |
|
||
export const generate = (key: string | number, params: Parameter): string | undefined => { | ||
if (params.style === "form") { | ||
return Core.generateFormParamter(key, params); | ||
return generateFormParamter(key, params); | ||
} | ||
return undefined; | ||
}; | ||
|
||
const generateFormParamter = (key: string | number, params: ParameterOfForm): string => { | ||
if (Guard.isEmpty(params.value)) { | ||
return `${key}=`; | ||
} | ||
if (Guard.isPrimitive(params.value)) { | ||
return `${key}=${params.value}`; | ||
} | ||
if (Guard.isArray(params.value)) { | ||
if (params.explode) { | ||
return params.value.map(item => `${key}=${item}`).join(";"); | ||
} else { | ||
return `${key}=${params.value.join(",")}`; | ||
} | ||
} | ||
if (Guard.isObject(params.value)) { | ||
if (params.explode) { | ||
return Object.entries(params.value) | ||
.map(([k, v]) => `${k}=${v}`) | ||
.join(";"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CookieParameterはURLSearchParams使うの良くなさそうなので旧実装もってきて分離しました。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
RFC6570上で特にCookie Parameterについて言及を自分は見つけられませんでした。他にこの変更をサポートする仕様はありますか? 参考 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. このPRはQueryParameterだけにフォーカスして、必要になったらCookie Parameterの方も調整しましょう 🙏 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 了解です |
||
} else { | ||
const value = Object.entries(params.value) | ||
.map(([k, v]) => `${k},${v}`) | ||
.join(","); | ||
return `${key}=${value}`; | ||
} | ||
} | ||
return `${key}=`; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,7 +47,7 @@ describe("QueryParameter - style:form", () => { | |
style: "form", | ||
explode: false, | ||
}); | ||
expect(result).toBe("color=blue,black,brown"); | ||
expect(result).toBe("color=blue%2Cblack%2Cbrown"); | ||
}); | ||
test("explode:true value:string[]", () => { | ||
const result = QueryParameter.generate("color", { | ||
|
@@ -67,7 +67,7 @@ describe("QueryParameter - style:form", () => { | |
style: "form", | ||
explode: false, | ||
}); | ||
expect(result1).toBe("color=R,100,G,200,B,150"); | ||
expect(result1).toBe("color=R%2C100%2CG%2C200%2CB%2C150"); | ||
}); | ||
test("explode:true value:object", () => { | ||
const result1 = QueryParameter.generate("color", { | ||
|
@@ -90,7 +90,7 @@ describe("QueryParameter - style:spaceDelimited", () => { | |
style: "spaceDelimited", | ||
explode: false, | ||
}); | ||
expect(result1).toBe("blue%20black%20brown"); | ||
expect(result1).toBe("color=blue+black+brown"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ここはOpenAPI Formatterの仕様と異なるようです。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. expect(result1).toBe("color=blue%20black%20brown"); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://qiita.com/masakielastic/items/61f5d9a215c62b55ccf2 |
||
}); | ||
test("explode:false value:object", () => { | ||
const result1 = QueryParameter.generate("color", { | ||
|
@@ -102,7 +102,7 @@ describe("QueryParameter - style:spaceDelimited", () => { | |
style: "spaceDelimited", | ||
explode: false, | ||
}); | ||
expect(result1).toBe("R%20100%20G%20200%20B%20150"); | ||
expect(result1).toBe("color=R+100+G+200+B+150"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. expect(result1).toBe("color=R%20100%20G%20200%20B%20150"); |
||
}); | ||
}); | ||
|
||
|
@@ -113,7 +113,7 @@ describe("QueryParameter - style:pipeDelimited", () => { | |
style: "pipeDelimited", | ||
explode: false, | ||
}); | ||
expect(result1).toBe("blue|black|brown"); | ||
expect(result1).toBe("color=blue%7Cblack%7Cbrown"); | ||
}); | ||
test("explode:false value:object", () => { | ||
const result1 = QueryParameter.generate("color", { | ||
|
@@ -125,7 +125,7 @@ describe("QueryParameter - style:pipeDelimited", () => { | |
style: "pipeDelimited", | ||
explode: false, | ||
}); | ||
expect(result1).toBe("R|100|G|200|B|150"); | ||
expect(result1).toBe("color=R%7C100%7CG%7C200%7CB%7C150"); | ||
}); | ||
}); | ||
|
||
|
@@ -140,6 +140,6 @@ describe("QueryParameter - style:deepObject", () => { | |
style: "deepObject", | ||
explode: true, | ||
}); | ||
expect(result1).toBe("color[R]=100&color[G]=200&color[B]=150"); | ||
expect(result1).toBe("color%5BR%5D=100&color%5BG%5D=200&color%5BB%5D=150"); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
こちらの関数利用したい場合はテストされるように
export
し、テストを実施してください。There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
把握です