As stated in the title.
Any arbitrary function can be expressed as $$f(x)=\frac{a_0}{2}+\sum^{\infty}_{n=1}(a_n\cos(nx)+b_n\sin(nx)) \tag{1}$$ where $$a_n=\frac{1}{\pi}\int_{-\pi}^{\pi}f(x)\cos(nx)dx \tag{2}$$ $$b_n=\frac{1}{\pi}\int_{-\pi}^{\pi}f(x)\sin(nx)dx \tag{3}$$
I am trying to show that (1)-(3) are consistent with $f(x)=e^{ix}=\cos(x)+i\sin(x)$.
However, for $a_n=\frac{1}{\pi}\int^{\pi}_{-\pi}e^{ix}\cos(nx)dx$ I got $a_n=0$, similarly, $b_n=0$.
What's gone wrong? Are the integral limits $\int_{-\pi}^{\pi}$ not valid in general, does it require modifications for non-periodic functions?
Let's try this definition: $f(x) = e^{ix}$ is the unique differentiable function $f : \mathbb R \to \mathbb C$ that satisfies $$ f'(x) = if(x),\qquad f(0)=1. \tag4$$ Then let's try to evaluate the required integrals $(2),(3)$ using integration by parts.
We will do this only using $(4)$. No complex exponentials appear in what I do below, so I will not accidentally use other properties of $e^{ix}$
Start with $(2)$, integrate by parts, use $(4)$ on the result. We get $$ a_n = \frac{-i}{n}\;b_n \tag5$$ Start with $(3)$ in the same way. We get $$ b_n = \frac{i}{n}\;a_n + \frac{(-1)^n}{\pi n}(f(-\pi)-f(\pi)) \tag6$$ Solving the system $(5),(6)$, we get (in case $n \ne 1$) $$ a_n = \frac{i n (-1)^n}{\pi(n^2-1)}\;(f(\pi)-f(-\pi)) \\ b_n = \frac{n(-1)^n}{\pi(n^2-1)}\;(f(-\pi)-f(\pi)) $$
Unless we can prove $f(\pi) = f(-\pi)$ from the definition $(4)$, it seems we will not be able to complete this.
Now consider $n=1$. We get equations $$ b_1 = ia_1 \\ a_1 = a_1 +\frac{i}{\pi}\;(f(\pi)-f(-\pi)) $$ so we conclude $f(\pi)-f(-\pi) = 0$. This tells us $a_n = b_n = 0$ for $n \ne 1$. Write $A = a_1$. Putting this all into $(1)$ we have $$ f(x) = A\big(\cos(x) + i\sin(x)\big) \tag7$$ Finally, we evaluate the constant $A$. For that we use $f(0)=1$ in $(7)$ to get $A=1$.